概述
主要存在两个问题
- listview只显示一个子item
- 滑动冲突
显示不正常问题解决办法有2个
- 动态测量item数目,设置高度
/**
* 动态设置listview高度
*/
public void setListViewHeight(){
Adapter adapter = listView.getAdapter();
if (adapter==null){
return;
}
int totalHeight = 0;
for (int i = 0; i < listView.getCount(); i++) {
View itemView = adapter.getView(i,null,listView);
itemView.measure(0,0);
totalHeight+=listView.getDividerHeight()+itemView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
}
- 重写onMeasure()方法,设置属性为AT_MOST
public class Mylistview extends ListView {
public Mylistview(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expanSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expanSpec);
}
}
存在问题:进入页面的时候,是从ListView开始的,ScrollView上面没有显示,要下拉的时候才显示。
解决方案:在ScrollView 的孩子根布局加上:
android:focusable="true"
android:focusableInTouchMode="true"
显示冲突原因分析如下图
参考地址
从源码角度解析ListView和ScrollView的嵌套冲突