我在寫一個顯示域模型列表的應用程序。默認行爲是在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