我正在编写一个显示域模型列表的应用程序。默认行为是在 ListView
中显示这些模型。我计划让用户使用 GridView
在将此数据视为列表或网格之间切换。这样做的惯用方法是什么?
我的域模型是一个简单的 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
。
我的 Activity 布局相当简单:
<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>
最佳答案
为什么不总是使用 gridview,并使用“ListView”模式将列设置为一个
android:numColumns
或 setNumColumns(int)
通过代码?
更多信息:http://developer.android.com/reference/android/widget/GridView.html
关于java - 如何在具有相同 ArrayAdapter 的同一 Activity 中将 GridView 与 ListView 交换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16323639/