【问题标题】:Scrolling two listview inside one LinearLayout在一个 LinearLayout 中滚动两个列表视图
【发布时间】:2015-11-21 07:05:34
【问题描述】:

我试图在一个线性布局内滚动列表视图,但它没有按我的意愿工作。我附上一张图片以获得更多解释。 这是我的布局

 <android.support.v7.widget.CardView
            android:id="@+id/card"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:clickable="false"
            app:cardBackgroundColor="@android:color/white"
            app:cardElevation="2dp"
            app:cardPreventCornerOverlap="false"
            app:cardUseCompatPadding="false">

            <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                    <ListView
                        android:id="@+id/listView1"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" />

                    <ListView
                        android:id="@+id/list"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"

                        />


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dip"
                        android:layout_weight="1"
                        android:gravity="center|bottom"
                        android:orientation="vertical">

                        <Button
                            android:id="@+id/findSelected"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal|center"
                            android:background="@drawable/flat_selector_green"
                            android:text="Next"
                            android:textColor="@android:color/white" />
                    </LinearLayout>

                </LinearLayout>
            </ScrollView>


        </android.support.v7.widget.CardView>

图片说明

我知道这不是使用列表视图的好方法,但我必须这样做。

【问题讨论】:

  • 你的意思是你想要 list1 和项目 1,2 list2 和项目 3,4,5,6,7 =>list 1,2,3,4,5,6,7?跨度>
  • @tinysunlight 是的,我希望所有值首先滚动,然后第二个列表元素滚动
  • 为什么需要ScrollViewListViews 自己滚动。
  • 您可以将所有数据放在一个列表中。为什么不将它们放在一起?
  • @MikeM。当我删除滚动视图时,我的第二个列表是滚动的,但不是第一个

标签: android android-layout android-listview android-xml android-scrollview


【解决方案1】:

只需从您的 xml 代码中删除 ScrollView,因为 ListView 有自己的 Scroll 不需要 ScrollView。然后代码将按您的意愿工作。在显示第一个 ListView 的每个项目后,将显示第二个列表。

【讨论】:

  • 感谢@Gaurav,但是当我这样做时,我的第二个列表是滚动而不是第一个
【解决方案2】:

根据android文档

您永远不应该将ScrollView 与 ListView 一起使用,因为 ListView 负责自己的垂直滚动。最重要的是,这样做会破坏 ListView 中处理大型列表的所有重要优化,因为它有效地强制 ListView 显示其整个项目列表以填充 ScrollView 提供的无限容器。

首先您将用户卡片视图作为最高父级,似乎您将使用它作为列表视图的子级,如果我的猜测是正确的,那么使用两个列表视图作为子级是不明智的。

第二在任何情况下,如果您想在同一个视图中使用两个列表视图,建议使用视图(TextView 等)将它们分开。

解决办法

1- 删除无用的滚动条。

2- 为您的列表视图添加权重,因为在您的案例中包装内容意味着整个视图。

3- 建议添加视图以分隔列表。

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:clickable="false"
app:cardBackgroundColor="@android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wholeview"
        >

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

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"

        />
            <!-- recommended to add view here -->

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <Button
                android:id="@+id/findSelected"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center"
                android:text="Next"
                android:textColor="@android:color/white"
                />
        </LinearLayout>
        </RelativeLayout>

【讨论】:

  • 感谢@MinaFawzy,但是当我尝试这个解决方案时,我的列表视图分为两个 eqaul 部分,我希望列表滚动一次
  • 第一个列表在第一部分滚动,第二个在第二部分滚动,但实际上我希望我的第一个列表滚动完成然后我的第二个自动滚动
  • 为什么不把所有元素放在一个列表视图中
  • 实际上我正在处理一个旧项目,其中维护两个选项卡并维护两个列表,但现在需要更改项目以删除选项卡并显示单个列表,问题是两者列表具有不同的适配器和模型,并且选择的项目已转到不同的 url。
猜你喜欢
  • 2013-01-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多