Android - RecyclerView/Gridview height is stretching

Viewed 189

Just trying to create a simple grid 2 row view like screen but the height of all my nested views are stretching. Kind of looks like it's stretching's to the height of the screen. I'm not 100% why it's happening. Thanks in advance!

This is my MainActivity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loadAdapter(listOf("URL_1", "URL_2"))
    }

    private fun loadAdapter(images: List<String>){
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView?.layoutManager = GridLayoutManager(this, 2)
        recyclerView?.adapter = GridViewAdapter(this, images)
    }
}

My adapter:

class GridViewAdapter(
    private val context: Context,
    private val dataSet: List<String>
) :
    RecyclerView.Adapter<GridViewAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imageView: ImageView = view.findViewById(R.id.imageView)

        init {

        }
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.view_image, viewGroup, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        Glide.with(context).load(dataSet[position]).into(viewHolder.imageView)
    }

    override fun getItemCount() = dataSet.size

}

The recyclerview view cell:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:cardBackgroundColor="@android:color/transparent"
    android:layout_margin="8dp"
    app:layout_constraintDimensionRatio="H,1:1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />
</androidx.cardview.widget.CardView>

MainActivity layout:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
1 Answers

There is actually a section about using Glide in connection with Recyclerview in the official Glide documentation: https://bumptech.github.io/glide/int/recyclerview.html.

They suggest to use PreloadSizeProvider to make sure that the cell will have the correct size.

I had the same problem for showing 3 squared images per row which means the cell height is constrained by the cell width. The problem went away when I was providing a placeholder (color).

Glide.with(imageView).load(uri).placeholder(R.color.white).into(imageView)

It seems to me that setting a (dimensionless) placeholder, the height is calculated based on the width (as expected as it should be a square). Once the image is loaded, it will be put into cell and resized based on the cell's dimensions. I consider this more like a workaround.

Related