ScrollView中放置ListView

最近有个需求是需要在ScrollView中放置一个ListView,如果不做处理的话,直接放置控件会导致:

  1. ListView的高度设置为wrap_content时,每个item的高度无法控制,可能导致item高度为0而不显示。
  2. ListView的高度设置为wrap_content时,疯狂调用getView方法导致卡顿。
  3. ListView的高度设置了合适的高度后,ListView无法滑动。

其对应的解决方法:

  1. 在ListView调用了setAdapter或者notifyDataChange后,使用该方法重新设置ListView的高度。

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;
    
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            //获取每个item的view
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new AbsListView.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT));
    
            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            //计算总高度
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        //加上divider的高度
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
    

    该方法原理就是通过adapter的getView方法返回item的view,计算每个item的高度height乘以item的个数,最后加上divider的高度。(如果知道每个item的高度,可以直接获取adapter的count乘以高度即可,省区调用getView方法的时间)

    使用该方法需要注意:
    1. item的根布局是必须是LinearLayout等重写了`onMeasure`方法的layout,因为获取高度时用到了`view.getMeasuredHeight()`方法.
    2. 该方法会在Adapter的getView方法前被调用。
    
  2. 在ListView外部放置一层单独的FrameLayout(性能最高),设置Layout的高度为wrap_content,然后设置ListView的高度为fill_parent,最后在setAdapternotifyDataChange后修改setListViewHeightBasedOnChildren方法,通过该方法返回带有正确高度的LinearLayout.LayoutParams(你没有看错,用LinearLayout.LayoutParams来设置FrameLayout),以此来设置FrameLayout的高度。这样不会疯狂调用getView方法了。

  3. 在onTouch事件中取消父布局的touch劫持。

    listView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });
    

如果使用了Github开源项目-PullToRefreshListView,它有一个headView和一个BottomView,所以它计算高度的方法会不同:

public static void setListViewHeightBasedOnChildren(PullToRefreshListView listView) {
    ListAdapter listAdapter = listView.getRefreshableView().getAdapter();
    if (listAdapter == null)
        return;

    int totalHeight = 0;
    View view = null;
    //去掉头尾两个view
    for (int i = 1; i < listAdapter.getCount()-1; i++) {
        view = listAdapter.getView(i, view, listView);
        totalHeight += view.getLayoutParams().height;
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getRefreshableView().getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

这里使用的是LayoutParams的height作为item的高度,所以在adapter的getView方法中,应该这样初始化:

public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.parenting_listview_item,parent,false);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值