1

I am setting images and video from local phone storage to recyclerview with Staggered LayoutManager with kotlin. Every thing is fine but on first launch recyclerview shows all images and video in only one column(on right side only) , Everything is normal on refershing on recyclerview. I have set span count 2 and using glide. I am new with this, thanks in advance.

Here is some code.

        recyclerview?.setHasFixedSize(true)
        recyclerview.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        adapter = ImageAdapter(activity)
        recyclerview?.adapter = adapter
        adapter?.notifyDataSetChanged()


Layout for recyclerview:

      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />

     </RelativeLayout>
Guest88
  • 11
  • 1

1 Answers1

1

Try changing width and height from match_parent to wrap_content :

  <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img"
        android:layout_width="100dp"
        android:layout_height="180dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

 </RelativeLayout>

Btw this is how I made it work:

<?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:id="@+id/rv_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/rv_image"
        android:layout_width="match_parent"
        android:layout_height="278dp"
        android:layout_margin="0dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Priyank-py
  • 349
  • 3
  • 14