AndroidScrollView与ListView的冲突问题

来源:互联网 发布:java else 必须 编辑:程序博客网 时间:2024/06/06 03:41
<ScrollView        android:id="@+id/ScrollView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#ffffffff"        android:scrollbars="vertical" >        <LinearLayout            android:id="@+id/tmall"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="文本备忘"                android:textSize="28sp" />            <ListView                android:id="@+id/listview0"                android:layout_width="fill_parent"                android:layout_height="wrap_content" />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="智能备忘"                android:textSize="28sp" />            <ListView                android:id="@+id/listview1"                android:layout_width="fill_parent"                android:layout_height="wrap_content" />        </LinearLayout>    </ScrollView>



public class ListViewUtils {    public void setListViewHeightBasedOnChildren(ListView listView) {        // 获取ListView对应的Adapter        ListAdapter listAdapter = listView.getAdapter();        if (listAdapter == null) {            return;        }        int totalHeight = 0;        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目            View listItem = listAdapter.getView(i, null, listView);            listItem.measure(0, 0); // 计算子项View 的宽高            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度        }        ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));        // listView.getDividerHeight()获取子项间分隔符占用的高度        // params.height最后得到整个ListView完整显示需要的高度        listView.setLayoutParams(params);    }}

private void initView() {        textListView = (ListView) findViewById(R.id.listview0);        intelListView = (ListView) findViewById(R.id.listview1);                textListView.setAdapter(new MyTestAdapter(this));        intelListView.setAdapter(new MyTestAdapter(this));                new ListViewUtils().setListViewHeightBasedOnChildren(textListView);        new ListViewUtils().setListViewHeightBasedOnChildren(intelListView);    }


0 0