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

来源:互联网 发布:linux命令wc l 编辑:程序博客网 时间:2024/06/04 00:23
多个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
原创粉丝点击