简体   繁体   中英

Android RecyclerView - StaggeredGridLayoutManager messes up width when scrolling

I have items with different height and I want the width always to match the parent.

When I set the RecyclerView it looks like this:

在此处输入图像描述

which is what I want, but when I start scrolling it messes up the width of the items below and looks like this:

在此处输入图像描述

This is the code:

val manager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
manager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS

recyclerViewGallery.adapter = adapterMemes
recyclerViewGallery.layoutManager = manager

and XML of itemview:

<?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>

The images are loaded from url so I tried to "reset" each item after they are loaded:

Glide
    .with(context)
    .load(thumb)
    .into(holder.image)

holder.image.post {
    holder.image.requestLayout()
}

so what's wrong here? Android is such a broken mess

Ok this is what I've changed to make it work:

android:layout_width="fill_parent"
android:adjustViewBounds="true"

on the ImageView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM