您的位置:首页 > 其它

解决scrollview嵌套listview ,listview异常,数据不显示,高度异常,布局异常

2017-07-07 14:17 561 查看
我们在开发中会经常遇到一些嵌套使用,但是每个嵌套不是简单的嵌套就能正常使用的,博主作为一个菜鸡在最初的学习阶段 真是难啊!

一遇到不正常的 现象,又不崩溃,打日志没问题,打debug没问题,参看xml文件没问题,可就为啥啥都没有呢。以为网络数据没加载出来,

。只怪自己太菜。

嵌套使用需谨慎啊,可能事件分发冲突,也可能导致布局, UI,异常。 本次 提供一个解决 ListView和ScrollView  方法。

/**
* Created by shangbeibei on 2016/12/13.
* <p>遇到ScrollView和ListView 计算高度</p>
*/
public class SetListViewHeightUtils {
public static SetListViewHeightUtils setHeight;

public  static synchronized SetListViewHeightUtils getInstence(){
if (setHeight ==null){
setHeight =new SetListViewHeightUtils();
}
return setHeight;
}
/**[注意] 此方法应当在所有adapter 操作之后 在执行
* <p>因为ListView和ScrollView(PullToZoomScrollViewEx)的 原因,发现在ScrollView(PullToZoomScrollViewEx)中嵌套ListView空间,
* 无法正确的计算ListView的大小,故可以通过代码,根据当前的ListView的列表项计算列表的尺寸</p>
* ps:也可以直接试用 PullToZoomListViewEx
*/
public void initListViewHight(ListView listview) {
ListAdapter listadapter = listview.getAdapter();
if (listadapter == null) return;
int totalHeight = 0;
for (int i = 0; i < listadapter.getCount(); i++) {
View listItem = listadapter.getView(i, null, listview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();// getMeasuredHeight 获取View的实际高度
}
ViewGroup.LayoutParams param = listview.getLayoutParams();
param.height = totalHeight + (listview.getDividerHeight() * (listadapter.getCount() - 1));
listview.setLayoutParams(param);
}

}


下一次:提供一个解决事件分发冲突的 解决办法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐