【问题标题】:How to add 2 ListViews in ScrollView如何在 ScrollView 中添加 2 个 ListView
【发布时间】:2016-10-03 12:03:14
【问题描述】:

这是我的代码,它只给我一个ListView,如果我将ListViewheight 设置为wrap_contentmatch_parent。但是,如果我根据dp 定义高度,它就会开始向我显示ListView。但在那种情况下看起来并不好。

<ScrollView  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"
    android:fillViewport="true">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="ListView1"
            android:gravity="center_vertical"
            android:paddingLeft="16dp"
            android:layout_marginTop="45dp"
            android:textColor="@color/white"
            android:textStyle="bold" />

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lv_upcoming"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:divider="@null" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#ffffff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="List Number 2"
            android:gravity="center_vertical"
            android:textColor="@color/white"
            android:fontFamily="sans-serif-condensed"
            android:textSize="18sp"
            android:id="@+id/tvPrevious"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

       <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/list_view_2"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:divider="@null"/>

    </LinearLayout>
</ScrollView>

请指导我如何在布局中添加多个ListView

【问题讨论】:

  • 绝对不能将 Listviews 放入 ScrollViews 中。效果不好。这是一个非常糟糕的选择。真的,换个方式想想吧。
  • 尝试使用NestedScrollView 而不是ScrollView

标签: android android-layout listview android-fragments


【解决方案1】:

始终建议在单个布局文件中使用单个 ListView。但是,是的,在某些情况下,您可能需要容纳两个列表。所以在这种情况下,我建议将两个列表合并到一个 ArrayList 中,以便它可以显示在一个 ListView 中。

现在还有其他方法可以在布局中显示多个 ListView。尝试使用NestedScrollView 而不是ScrollView

如果您也考虑使用RecyclerView here's an implementation 在同一个RecyclerView 中显示多个列表。您可以在 code section 中找到该项目,并且该 wiki 也有很好的文档记录。

【讨论】:

  • 谢谢,我尝试使用NestedListView,它显示了两个列表项,但它减小了列表项的大小,并且在列表视图中只显示了一项。我尝试同时使用wrap_contentmatch_parent。如果我定义,比如说200dp,它会显示 2 个项目,但列表视图中不再有滚动视图
  • 不是NestedListView .. 它实际上是ListViewNestedScrollView 内。无论如何,我宁愿建议你去RecyclerView 实现。它简单易行。
【解决方案2】:

将 ListView 放入 FrameLayout 并设置 layout_weight 为 1。

    <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"
    android:fillViewport="true"
    android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="ListView1"
            android:gravity="center_vertical"
            android:paddingLeft="16dp"
            android:layout_marginTop="45dp"
            android:textStyle="bold"
            android:textColor="@color/white"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/lv_upcoming"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:divider="@null"
                />
    </FrameLayout>

    <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#ffffff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="List Number 2"
            android:gravity="center_vertical"
            android:fontFamily="sans-serif-condensed"
            android:textSize="18sp"
            android:id="@+id/tvPrevious"
            android:paddingLeft="16dp"
            android:textColor="@color/white"
            android:paddingRight="16dp" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/list_view_2"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:divider="@null"
            />
    </FrameLayout>

</LinearLayout>

【讨论】:

    【解决方案3】:

    在滚动视图下使用列表视图不是一个好习惯,如果你想使用它也一样。然后你必须记住滚动视图下的列表视图不能很好地工作,你必须以编程方式管理列表视图的高度。当您在两个 list-view 上设置适配器时,您会遇到此问题。下面给出了相同的解决方案,我在下面的链接中看到了:-

    Android ListView rows in ScrollView not fully displayed - clipped

    public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter(); 
            if (listAdapter == null) {
                // pre-condition
                return;
            }
    
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多