android - 带有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 行之间的巨大差距
解决方案
我建议您应该修复此 TextView 的 maxLine 并确保加载到 ImageView 中的图像高度相同,或者您应该修复 ImageView 的高度。
推荐阅读
- reactjs - 如何测试是否在 Enzyme 和 Jest 中设置了状态
- firebase - 如何根据数据库模拟器更新触发 Firebase 函数模拟器
- wordpress - Wordpress docker 将主题复制到暴露的文件夹中
- java - java.io.StreamCorruptedException:无效的流标头:来自 ObjectInputStream 的 410D0AAC
- aerospike - 使用 Aerospike 处理由键组合定义唯一性的数据
- sql - 返回具有相同 id 的行和单独的列都包含相同的值
- android - 如何在滚动时隐藏 CoordinatorLayout 内的 LinearLayout?
- sql - 在多个条件下使用 case-when 进行复杂查询
- c++ - 如何在 c++ 中的同一个数组中工作时随机化 int 一定次数?
- postgresql - 使用 COPY 但表有额外的列?