1

I have a recyclerview with a GridLayoutManager as LayoutManager:

XML:

    <androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/recycler_view_container_anime"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_anime"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:orientation="vertical"
        android:background="#FF0000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

JAVA:

recycler_view_anime = view.findViewById(R.id.recycler_view_anime);
    recycler_view_anime.setHasFixedSize(true);
    LinearLayoutManager layoutManager_anime_crucial = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
    recycler_view_anime.setLayoutManager(layoutManager_anime_crucial);
    recycler_view_anime.setItemAnimator(new DefaultItemAnimator());

Recycler's elements are image with fixed width and height:

XML for recyclerview elements:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_container"
android:layout_width="100dp"
android:layout_height="230dp">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/card_view_id"
    android:background="#131313"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/main_image"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:src="@drawable/default_image"
        android:scaleType="fitXY"
        android:transitionName="example_transition"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintStart_toStartOf="parent"
        card_view:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout14"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        card_view:layout_constraintBottom_toBottomOf="parent"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintStart_toStartOf="parent"
        card_view:layout_constraintTop_toBottomOf="@+id/main_image">

        <TextView
            android:id="@+id/main_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:ellipsize="end"
            android:fontFamily="@font/futura_book"
            android:gravity="center_horizontal|center_vertical"
            android:maxLines="2"
            android:text="Spyyyyyyyyyyyyyyyyyyyyyyyjjjjjjjjjjjjjjj"
            android:textColor="@color/white"
            android:textSize="12dp"
            card_view:layout_constraintBottom_toBottomOf="parent"
            card_view:layout_constraintEnd_toEndOf="parent"
            card_view:layout_constraintStart_toStartOf="parent"
            card_view:layout_constraintTop_toTopOf="parent">

        </TextView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Doing like this what I get is:

enter image description here

What I'm trying to achieve:

enter image description here

(Red background is the recyclerview background, to show that recycler width works fine, the problem is the gridlayoutmanager)

Basically I need to space equally between elements but also on the left and on the right of the first and last elements.

I searched a lot and tried a lot of recyclerview.itemdecorator(...) solutions but none of them worked. It's like if the gridlayoutmanager itself is not centered inside the recycler view width...

EDIT: the recyclerview that I use it's inside a viewpager, so the recycler's xml is a single xml file, maybe this is the problem, because I tried to set in this xml the width as wrap and in was centered with no space and the itemdecorator worked fine, but once I returned to match_parent it broke again

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
C-Gian
  • 62
  • 2
  • 19
  • Please, check the answers for [this question](https://stackoverflow.com/questions/28531996/android-recyclerview-gridlayoutmanager-column-spacing?answertab=scoredesc#tab-top), it may help. – Marcos Narvaez Dec 20 '22 at 15:32

1 Answers1

0

GridSpacingItemDecoration might be what you are looking for, try tweaking the spacing parameter. Also, Set the includeEdge parameter to true for adjusting the edge spacing. That is what helped me.

  • tweaking with the spaceing parameter solved it but it's not a good pratice, on different phone size the rror is not fixed – C-Gian Dec 20 '22 at 16:18
  • 1
    @C-Gian You are correct, but we don't have complete control on gridLayoutManger. you should use different layouts to support different screen sizes. – VipulSharma Dec 20 '22 at 17:37
  • any suggestions? there is something that act like a gridlayout? – C-Gian Dec 20 '22 at 21:59
  • 1
    there is no replacement for grid layout for now, you can use qualifiers and make different layouts with same name as your default layout – VipulSharma Dec 21 '22 at 07:32