在Android开发中,如果外层使用ScrollView嵌套ListView(ExpandableListView),以下统一称为ListView,会导致ListView的显示高度变窄,甚至不能实现屏幕外内容的括展,那么滑动冲突就出现了。
解决思路:
思路一:
- 在XML中将高度固定(如果ListView中的item不够多,或者要显示的内容没有超出屏幕可以使用)
- 在代码中将高度固定,如下所示:
-
public void getHeight(int height){ LayoutParams params=listView.getLayoutParams(); params.width=LayoutParams.MATCH_PARENT; params.height=height; }
思路二:
在代码中根据二级条目的个数,动态添加二级条目的高度
public void setHeight(){
int height=0;
int count=mAdapter.getCount();//如果是ExpandableListView-->count=getGroupCount();
for(int i=0;i<count;i++){
View view=mAdapter.getView(i,null,mListView);//如果是ExpandableListView-->mAdapter.getGroupView(i,null,mExpandableListView);
view.measure(0,0);
height+=view.getMeasuredHeight;//获取onMeasure过程中要显示的子条目的高度,从第i个条目开始增加高度
}
LayoutParams params=mListView.getLayoutParams();
params.width=LayoutParams.MATCH_PARENT;
params.height=height;
mListView.setLayoutParams(params):
}
注意:setHeight()方法要放在第一级条目的点击事件中
下面是部分完整的代码-->
boolean isExpand =false;
final int count=mExpandAdapter.getGroupCount();
int height1=0;
int height2=0;
for(int i=0;i<count;i++){
final View view=mExpandAdapter.getGroupCount(i,null,mExpandableListView);
view.measure(0,0);
height2=view.getMeasuredHeight();
height1+=view.getMeasuredHeight();
}
LinearLayout.LayoutParams params=mExpandableListView.getLayoutParams();
param.width=LinearLayout.LayoutParams.MATCH_PARENT;
params.height=height1;
mExpandableListView.setLayoutParams(params);
final int finalHeight=height1;
final int finalHeight2=height2;
mExpandableListView.setOGroupClickListener(new ExpandableListView.OnGroupClickListener(){
@Override
public boolean onGroupClick(ExpandableListView parent,View v,int groupPosition,long id){
int childSize=parentList.get(groupPosition).getChildList().size();
if(!isExpand){
LinearLayout.LayoutParams params=mExpandableListView.getLayoutParams();
params.width=LinearLayout.LayoutParams.MATCH_PARENT;
params.height=finalHeight+finalHeight*childSize;//*******重点 1
mExpandableListView.setLayoutParams(params);
}else{
LinearLayout.LayoutParams params =mExpandableListView.getLayoutParams();
params.width=LinearLayout.LayoutParams.MATCH_PARENT;
params.height=finalHeight;//********重点 2
mExpandableListView.setLayoutParams(params);
}
return false;
});
最好是亲自敲一下代码感受下.