2013-05-01 76 views
1

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

不要共享適配器,共享數據。爲網格和列表提供不同的適配器,併爲兩個適配器使用相同的備份數據。 – kcoppock 2013-05-01 18:37:33

回答

5

爲什麼不總是使用GridView,並設置列一個使用通過代碼

android:numColumnssetNumColumns(int)你「的ListView」模式?

更多信息:http://developer.android.com/reference/android/widget/GridView.html

+0

我明白了。這看起來不錯!我如何在設置numColumns時交換行佈局?我需要2個不同的表示和列表。 – 2013-05-01 18:22:43

+0

啊。爲此,您可能需要編輯適配器類並添加一個方法來將句柄/引用交換爲要使用的佈局文件。您也可以繼承GridView並重寫'setColumns',然後調用新的CustomArrayAdapter方法來交換佈局句柄/引用。然後調用它的notifyDataSetChanged() – petey 2013-05-01 18:27:27

相關問題