[英]Android RecyclerView - StaggeredGridLayoutManager messes up width when scrolling
我有不同高度的項目,我希望寬度始終與父項匹配。
當我設置 RecyclerView 時,它看起來像這樣:
這就是我想要的,但是當我開始滾動時,它會弄亂下面項目的寬度,看起來像這樣:
這是代碼:
val manager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
manager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
recyclerViewGallery.adapter = adapterMemes
recyclerViewGallery.layoutManager = manager
和 itemview 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/galleyImage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:background="@drawable/round_corners"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_gallery" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
圖像是從 url 加載的,所以我在加載后嘗試“重置”每個項目:
Glide
.with(context)
.load(thumb)
.into(holder.image)
holder.image.post {
holder.image.requestLayout()
}
那么這里有什么問題呢? Android 真是一團糟
好的,這就是我為使其工作所做的更改:
android:layout_width="fill_parent"
android:adjustViewBounds="true"
在 ImageView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.