我在写一个显示域模型列表的应用程序。默认行为是在ListView
中显示这些模型。我计划让用户在使用GridView
将这些数据视为列表或网格之间切换。这样做的惯用方法是什么?如何在具有相同ArrayAdapter的同一个Activity中将GridView与ListView交换?
我的域模型是一个简单的POJO,我为它和一个自定义行布局目前看起来像这样创造了一个CustomArrayAdapter
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label" >
</TextView>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/label"
android:text="@+id/text" />
</RelativeLayout>
我希望用同样的ArrayAdapter
两个布局。
我活动的布局很简单:
<RelativeLayout 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:gravity="center_vertical"
tools:context=".MainActivity" >
<!-- what do I put here? -->
</RelativeLayout>
不要共享适配器,共享数据。为网格和列表提供不同的适配器,并为两个适配器使用相同的备份数据。 – kcoppock 2013-05-01 18:37:33