Sir
Sir

Reputation: 109

ImageViews in RecyclerView not "weight" aligned horizontally

This is how it looks in the preview and on the device.

Rows with different spacing

The code for the row layout and the RecyclerView layout is as base as it gets

Row has a single ImageView:

<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 = "175dp"
    android:layout_height = "175dp"
    android:layout_margin = "4dp">

    <ImageView
        android:id = "@+id/imageView"
        android:layout_width = "0dp"
        android:layout_height = "0dp"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent"
        tools:ignore = "ContentDescription"
        tools:srcCompat = "@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>

The RecyclerView (which is the only view in that fragment) is a Grid with 2 span:

    <androidx.recyclerview.widget.RecyclerView
        android:id = "@+id/recyclerView"
        android:layout_width = "0dp"
        android:layout_height = "0dp"
        app:layoutManager = "androidx.recyclerview.widget.GridLayoutManager"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent"
        app:spanCount = "2"
        tools:listitem = "@layout/row_dir_observer_secondary" />

I have no idea what is wrong, tried every gravity related layout setting, tried weights, but giving padding_start to the parent was the only thing that worked.

Upvotes: 0

Views: 66

Answers (2)

Chandan Sharma
Chandan Sharma

Reputation: 2943

When you use GridLayoutManager as LayoutManager in RecyclerView, don't put fixed size width, because if you want each and every item of the RecyclerView has same width then GridLayoutManager Automatically divide screen width according to SpanCout, if you use width as match_parent.

So first remove hard-coded width value from adapter's layout file (example here is 175dp) and set to match_parent width in Adpater layout file.

Try this,

<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 = "175dp"
    android:layout_margin = "4dp">

    <ImageView
        android:id = "@+id/imageView"
        android:layout_width = "0dp"
        android:layout_height = "0dp"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent"
        tools:ignore = "ContentDescription"
        tools:srcCompat = "@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Ashish Sharma
Ashish Sharma

Reputation: 576

You've used hardcoded width in adapter, use match_parent, I think you're already using gridlayoutmanager with span = 2, so not need to change anything there.

Upvotes: 1

Related Questions