[英]recyclerview GridLayoutManager spaces not divided equally
I have a recyclerview with a GridLayoutManager as LayoutManager:我有一个带有 GridLayoutManager 作为 LayoutManager 的 recyclerview:
XML: 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: Recycler 的元素是具有固定宽度和高度的图像:
XML for recyclerview elements: recyclerview 元素的 XML:
<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) (红色背景是recyclerview背景,显示recycler width工作正常,问题是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.我搜索了很多并尝试了很多 recyclerview.itemdecorator(...) 解决方案,但没有一个有效。 It's like if the gridlayoutmanager itself is not centered inside the recycler view width...
这就像 gridlayoutmanager 本身没有在回收站视图宽度内居中......
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编辑:我使用的 recyclerview 在 viewpager 中,所以回收器的 xml 是单个 xml 文件,也许这就是问题所在,因为我试图在这个 xml 中设置 wrap 和 in 的宽度居中,没有空间和 itemdecorator工作正常,但一旦我回到 match_parent 它又坏了
GridSpacingItemDecoration might be what you are looking for, try tweaking the spacing parameter. GridSpacingItemDecoration可能是您正在寻找的,请尝试调整间距参数。 Also, Set the
includeEdge
parameter to true for adjusting the edge spacing.此外,将
includeEdge
参数设置为 true 以调整边缘间距。 That is what helped me.这就是帮助我的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.