在Android中显示ListView中的所有项目

时间:2015-01-23 19:04:02

标签: android listview android-listview scroll

我已经开始构建一个有两个ListView的应用程序。

现在我把ScrollView放在它上面,因为itmes的大部分可能会变得非常大,因此我希望整个布局可滚动而不是列表本身。但我的解决方案有效!

btw:我不想用LinearLayout替换我的ListViews,因为那时我的所有适配器都不见了。

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

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

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

            <TextView
                style="@style/Heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ListView1" />

            <Space
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <Button
                style="@style/ButtonStyle.SpinnerAddButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/ic_add_black_24dp"
                android:onClick="addToList1" />

        </LinearLayout>

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/marginLeft"
            android:layout_marginRight="@dimen/marginLeft" />


    <!--Second list-->

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

            <TextView
                style="@style/Heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ListView2" />

            <Space
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <Button
                style="@style/ButtonStyle.SpinnerAddButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/ic_add_black_24dp"
                android:onClick="addToList2" />

        </LinearLayout>

        <ListView
            android:id="@+id/listView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/marginLeft"
            android:layout_marginRight="@dimen/marginLeft" />

    </LinearLayout>
</ScrollView>

我的布局现在显示彼此之下的所有ListView,但每个ListView只显示一个项目,然后您可以滚动浏览此列表。

1 个答案:

答案 0 :(得分:0)

您应该使用一个列表视图,并且只有不同的视图类型。

@Override
public int getItemViewType(int position) {
    return isFirstItem(position) ? TYPE_ITEMA : TYPE_ITEMB;
}

@Override
public int getViewTypeCount() {
    return 2;
}