网格视图中的内容重叠 android

Content overlapping in grid view android

我正在创建一个 android 应用程序,我想在其中以网格视图显示数据。我的网络服务运行良好,数据成功显示在网格视图中。现在我想要的是,当数据填充到第二行时,它会重叠在第一行。我该如何解决以下问题?

我的 activity.

中的 GridView
<GridView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:divider="@color/list_divider"
        android:numColumns="3"
        android:layout_marginTop="12dp"
        android:scrollbars="none"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:verticalSpacing="15dp"
        android:horizontalSpacing="15dp"
        android:dividerHeight="1dp"/>

自定义适配器布局代码如下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/layout_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="10dp">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="10dp"
        app:cardElevation="0dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp">

            <!-- Thumbnail Image -->
            <com.android.volley.toolbox.NetworkImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_alignParentLeft="true"
                android:foregroundGravity="center"
                android:scaleType="fitXY"
                android:src="@drawable/demo_image" />

            <ImageView android:id="@+id/imgYoutube"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/youtube"
                android:visibility="gone"
                android:layout_centerInParent="true"></ImageView>

        </RelativeLayout>

    </androidx.cardview.widget.CardView>

    <!-- Movie Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_marginTop="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="Moin Khan"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold" />

    <!-- Rating -->
    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="5dp"
        android:text="Complete course"
        android:textSize="12sp" />

</LinearLayout>

我认为您需要使用 RecyclerView 而不是 GridView 来达到您的目的,因为您有高度不等的项目,这通常会导致 gridview 出现问题。 将这些标签用于您的案例,

recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));