关于解决多个listView嵌套到scrollview中滑动冲突的办法

多个ListView嵌套到一个android UI界面中时会产生滑动冲突,主要原因是每个listView拥有自己的滑动条而造成,解决办法是为每一个listView固定一个高度.  下面介绍一个通用的方法来解决

empty
/*
    * 解决多个listView滑动冲突的问题
    * 在scollview中嵌套layout 这样才能插入组件
    * 使用scollview为界面插入滚动条
    *
    * 解决思路为固定listview的高度
    * */
    public void setListViewHeightBasedOnChildren(ListView listView){
        BaseAdapter listAdapter = (BaseAdapter) 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);  //在还没有构建View 之前无法取得View的度宽。 在此之前我们必须选 measure 一下.
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // params.height += 5;// if without this statement,the listview will be
        // a
        // little short
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);

    }

listView_head = (ListView) view.findViewById(R.id.list_item_peoplefragment_head);
listView_head.setAdapter(new listview_people_head_adapter(getActivity()));//context需要使用getActivity来获取
setListViewHeightBasedOnChildren(listView_head);

listView_a = (ListView) view.findViewById(R.id.list_item_peoplefragment_a);
listView_a.setAdapter(new listview_people_head_adapter(getActivity()));
setListViewHeightBasedOnChildren(listView_a);

listView_b = (ListView) view.findViewById(R.id.list_item_peoplefragment_b);
listView_b.setAdapter(new listview_people_head_adapter(getActivity()));
setListViewHeightBasedOnChildren(listView_b);
需要注意的是.在xml布局中  当scrollview中嵌套listView时要为其添加布局  例如

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值