如何滚动整个视图而不仅仅是listview?

时间:2016-06-13 21:31:09

标签: android listview scrollview

在android中,我想展示一个带有固定文本和动态列表的UI。我添加了一个滚动视图,但它只滚动listview(id是listview_name),而不是整个视图。我该如何解决这个问题?

这是我的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:padding="10dp"
            android:orientation="vertical"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

             <LinearLayout
                 android:orientation="horizontal"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content">

                <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

             </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="colorBlack"/>

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

                <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

                <ListView
                    android:id="@+id/listview_name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="colorBlack"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以在Scrollview中使用ListView,我尝试了以下方法来实现此目的。

 public 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++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, RelativeLayout.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

活动中的用法:setListViewHeightBasedOnChildren(yourlistview);