【问题标题】:ListView inside a ScrollView issue in AndroidAndroid 中的 ScrollView 问题中的 ListView
【发布时间】:2012-12-14 17:33:57
【问题描述】:

这是我的 XML 布局。我的滚动视图中有一个列表视图。如果我将列表视图高度更改为 wrap_content 它不起作用,我将手动设置高度。而且,如果数据增长超出其隐藏的限制。我看到很多答案来解决这个问题。为我的情况解决它的最佳选择是什么?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:focusableInTouchMode="true" >

    <LinearLayout
        android:id="@+id/store_scroller"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="#c6c6c6"
            android:padding="10dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                layout_alignParentLeft="true"
                android:text="Stores"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/merchant_count_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="0 Results" />
        </RelativeLayout>
        <Gallery
            android:id="@+id/gallery1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:gravity="left"
            android:listSelector="@color/gridviewlistselector"
            android:paddingLeft="10dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:spacing="10dp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="#c6c6c6"
            android:padding="10dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                layout_alignParentLeft="true"
                android:text="Products"
                android:textSize="20dp" />

            <Spinner
                android:id="@+id/filter_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_toRightOf="@+id/textView1" />

            <TextView
                android:id="@+id/product_count_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="0 Results" />
        </RelativeLayout>

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="4800dp"
            android:layout_weight="1"
            android:drawSelectorOnTop="true"
            android:listSelector="@color/gridviewlistselector"
            android:orientation="vertical" />
    </LinearLayout>
</LinearLayout>

</ScrollView>

【问题讨论】:

  • 请观看 Android 的主要设计师 discuss the best practices 与 UI 和 ListViews,这包括为什么你不应该尝试组合在同一方向滚动的 Views。
  • 原因是我有一个水平画廊和一个垂直的列表视图。当我向上滚动时,整个页面应该向上滚动,包括画廊。那我该如何解决我的案子呢?
  • 您可以做到这一点的唯一方法是画廊和 ListView 共享屏幕而不滚动。如果一个项目太大并且两者都不可见,那么您应该完全专注于不同的设计。 (我明白你想要什么,但这会造成糟糕的用户体验,而且编码是一件令人不快的事情。)

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


【解决方案1】:

简短回答:您不能(不应该)将 ListView 包裹在 ScrollView 中。

长答案:正如 Romain Guy(Google 自己的 UI 专家)曾经说过的:

使用 ListView 使其不滚动非常昂贵,并且违背了 ListView 的全部目的。你不应该这样做。只需使用 LinearLayout。

通过"How can I put a ListView into a ScrollView without it collapsing?"引用。

【讨论】:

  • 原因是我有一个水平画廊和一个垂直的列表视图。当我向上滚动时,整个页面应该向上滚动,包括画廊。那我该如何解决我的案子呢?
  • 尝试使用 listView 标题,并且在标题内部有一个水平视图寻呼机。
【解决方案2】:

您可以在 ScrollView 中使用列表视图代替,您可以在其中定义所需组件的单独布局。然后你可以使 ListView 的布局变得更复杂。您可以使用以下代码:

LayoutInflater inflater = LayoutInflater.from(this);
View LTop = inflater.inflate(R.layout.header_tolistview, null);
listview.addHeaderView(LTop); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2016-09-22
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多