Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to display image CardViews inside a RecyclerView. Because the images have different sizes I want to use StaggeredGridLayoutManager. The problem is that (I believe) after the items are recycled the StaggeredGridLayoutManager shuffles them. I have been looking for a solution and the only thing I could find is to add .setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE); to the layout manager, but when I dod that none of the items are displayed and it seems that the recyclerview is empty. However, it has items because I use this RecyclerView and the empty view is not displayed. (I had the same problem with the default RecyclerView as well, so problem is not with this custom RecyclerView)

This is my code:

mRecyclerView = view.findViewById(R.id.image_recyclerView);
ConstraintLayout mEmptyView = view.findViewById(R.id.empty_view);
mRecyclerView.setEmptyView(mEmptyView);
mImageAdapter = new ImageAdapter(mImages);
mRecyclerView.setAdapter(mImageAdapter);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE); if I add this none of the images are displayed
mRecyclerView.setLayoutManager(layoutManager);

Layout file:

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/DarkWhite">

    <com.example.app.EmptyRecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image_recyclerView"
        android:padding="4dp"
        android:background="@color/DarkWhite"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:textAlignment="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="20dp"
            android:text="Empty"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

This is how I load the images from the ViewHolder's bind method(If this information is relevant, they are getting loaded from assets):

Glide.with(context).load(mImage.getImgUri()).into(mImageView);

And this is where it is supposed to be loaded:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.MaterialComponents.Light"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="4dp"
    android:layout_margin="10dp">

    <ImageView
        android:id="@+id/image_imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>


</com.google.android.material.card.MaterialCardView>

How can I solve this?

question from:https://stackoverflow.com/questions/65645540/staggeredgridlayoutmanager-keeps-rearranging-items

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...