首页 > 解决方案 > 带有gridlayoutmanager的Recyclerview中的行之间的空间被弄乱了

问题描述

我有一个带有 gridlayoutmanager 的 recyclerview。列间距正确,但行混乱。每次我上下滚动时,每行之间的空间似乎都会发生变化。 显示问题的图片

这是我设置回收站视图和网格布局管理器的地方:

  var inflater = view.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    var rootLayout = view.findViewById<LinearLayout>(R.id.ll_linear)
    var heroListView = inflater.inflate(R.layout.hero_list_tab_card, rootLayout, true)

    //get the recycler view
    var heroListRv = heroListView.findViewById<RecyclerView>(R.id.rv_heroes)
    heroListRv.layoutManager = GridLayoutManager(view.context, 4)

这是 hero_list_tab_card:

<?xml version="2.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

       <TextView
           android:id="@+id/tv_primary_attribute"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="30sp"
           android:textAllCaps="true"
           android:textColor="@color/colorTextWhite"
           android:text="@string/test"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintStart_toStartOf="parent"/>

        <ImageView
            android:layout_width="60dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/tv_primary_attribute"
            app:layout_constraintEnd_toEndOf="parent"
            android:src="@drawable/dotabuddy_icon"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_heroes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            app:layout_constraintTop_toBottomOf="@id/tv_primary_attribute"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

以下是填充在网格内的实际英雄元素:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/iv_heroimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dotabuddy_icon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <TextView
        android:id="@+id/tv_heroname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorTextWhite"
        android:text="@string/test"
        app:layout_constraintTop_toBottomOf="@id/iv_heroimage"
        app:layout_constraintStart_toStartOf="@id/iv_heroimage"
        app:layout_constraintEnd_toEndOf="@id/iv_heroimage"/>

</androidx.constraintlayout.widget.ConstraintLayout>

如果您希望我发布任何其他信息,请告诉我。任何帮助将不胜感激。

编辑:为澄清起见,我在谈论第 4 行和第 5 行之间的巨大差距

标签: androidkotlin

解决方案


我建议您应该修复此 TextView 的 maxLine 并确保加载到 ImageView 中的图像高度相同,或者您应该修复 ImageView 的高度。


推荐阅读