线性布局内具有网格视图的列表视图适配器显示项目彼此下方而不是两列

Listview adapter with gridview inside linearlayout showing items below eachother instead of in two columns

我正在创建一个 ListView 自定义适配器,在线性布局中使用 gridview 来显示用户配置文件。

我的 XML 设计视图正确显示了它们。但是,当我 运行 应用程序时,它显示每个项目都在彼此下方,而不是分布在 2 列中。 知道我做错了什么吗?

Example

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:gravity="center" />

    <ImageView
        android:id="@+id/rimage"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_margin="10dp"
        android:contentDescription="@string/todo"
        app:srcCompat="@drawable/thumb_default_icon_female" />

    <TextView
        android:id="@+id/rnick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="@string/nick" />

    <TextView
        android:id="@+id/rcity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="@string/city" />
</LinearLayout>

正在显示 List Items

试试这个

 gridLayoutManagerVertical =
            new GridLayoutManager(this,
                    2, //The number of Columns in the grid
                    LinearLayoutManager.VERTICAL,
                    false);

首先,你们不应该使用 GridView,而应该使用 RecyclerView,因为它已经取代了旧的 ListView网格视图

为了实现您想要的效果,您需要创建跨度数为 2 的 GridLayoutManager

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);

documentation

您当前的列表项逻辑是这样的:

Item 1--> LinearLayout
        -->GridView
        -->ImageView
        -->TextView
        -->TextView

Item 2--> LinearLayout
        -->GridView
        -->ImageView
        -->TextView
        -->TextView

所以在实际情况下,您的项目是 LinearLayouts,占据了设备屏幕的整个宽度。

您应该使用 GridView 作为您项目的容器而不是 ListView,并且您应该实现您的 customAdapter 以使用 LinearLayout 项目:

您的 Activity 布局中的 GridView:

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:gravity="center" />

您的项目布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
    android:id="@+id/rimage"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_margin="10dp"
    android:contentDescription="@string/todo"
    app:srcCompat="@drawable/thumb_default_icon_female" />

<TextView
    android:id="@+id/rnick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:text="@string/nick" />

<TextView
    android:id="@+id/rcity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:text="@string/city" />

并将您的适配器直接设置为您的 GridView。确保您的适配器扩展了 BaseAdapter(而不是 ListAdapter);

yourGridView.setAdapter(yourCustomAdapter);

总而言之,更好的方法是将 RecyclerView 与 GridLayoutManager 一起使用。

recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(adapter);

Here is a simple tutorial

使用嵌套 LinearLayout 而不是 gridLayout,而且我认为您忘记将视图放在 gridLayout 中,因为它们在 gridLayout 和 LinearLayout 的子项之外,这就是为什么它们显示在另一个下方的原因。试试下面的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">



<ImageView
    android:id="@+id/rimage"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_margin="10dp"
    android:contentDescription="@string/todo"
    app:srcCompat="@drawable/thumb_default_icon_female" />
<LinearLayout
    android:id="@+id/grid_view"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >
<TextView
    android:id="@+id/rnick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:text="@string/nick" />

<TextView
    android:id="@+id/rcity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:text="@string/city" />
 </LinearLayout>
</LinearLayout>