【问题标题】:How to scroll whole view not just listview?如何滚动整个视图而不仅仅是列表视图?
【发布时间】:2016-06-13 21:31:09
【问题描述】:

在 android 中,我想展示一个带有固定文本和动态列表的 UI。我添加了一个滚动视图,但它只滚动列表视图(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>

【问题讨论】:

  • ScrollView 中不能有 ListView。这是Android本身的规则。我建议你拿列表 ScrollView。

标签: android listview scrollview


【解决方案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);

【讨论】:

  • 谢谢丽娜。有没有办法只在活动 xml 文件中编码?
  • 不,我在 xml 中找不到任何方法。
猜你喜欢
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多