简体   繁体   中英

recyclerview GridLayoutManager spaces not divided equally

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:

在此处输入图像描述

What I'm trying to archieve:

在此处输入图像描述

(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 brokes again

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM