java - 如何在具有相同 ArrayAdapter 的同一 Activity 中将 GridView 与 ListView 交换?

标签 java android android-layout

我正在编写一个显示域模型列表的应用程序。默认行为是在 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:numColumnssetNumColumns(int) 通过代码?

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

关于java - 如何在具有相同 ArrayAdapter 的同一 Activity 中将 GridView 与 ListView 交换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16323639/

相关文章:

java - 如何在Spring中加载整个文件夹的xml文件进行配置?

android - Facebook SDK 并使用 ShareDialog 问题共享 Play Store 应用程序链接

java - 在 xml 中为 Android 中的自定义 View 设置 onClick 在某些版本上不起作用

android - 如何在没有填充的情况下在android中获得准确的命中矩形?

Java 枚举方法 - 返回相反方向的枚举

java - 如何避免初始化大数组

android - 如果recyclerview为空android如何显示textview

android - 如何处理回收站 View 中的空数据?

java - 在 Java 中创建简单模型验证的好方法

android - NullPointerException 和 Intent Android