简体   繁体   English

带有GridLayoutManager的RecyclerView-布局项目

[英]RecyclerView with GridLayoutManager - layouting items

I have a simple layout with a recycler. 我的回收站布局很简单。 I feed it with Adapter and set GridLayoutManager for the recycler. 我用Adapter喂它,并为回收站设置GridLayoutManager

<android.support.v7.widget.RecyclerView
android:id="@+id/scripts_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:verticalSpacing="20dp"
android:clipToPadding="false"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
tools:listitem="@layout/tmpl_script"
android:paddingBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:paddingTop="30dp"
app:layout_gravity="center" />

Activity 活动

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.scripts_recycler_view);
recyclerView.setAdapter(adapter);
int columns = getResources().getInteger(R.integer.scripts_columns);
recyclerView.setLayoutManager(new GridLayoutManager(this, columns));
recyclerView.setHasFixedSize(true);

Item 项目

<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@drawable/game_border"
android:gravity="center"
android:orientation="vertical">

I have two problems: 我有两个问题:

  1. there is no space between rows 行之间没有空格
  2. items are not centered within the row 项目不在行内居中

I tried all properties in Android Studio but they do not have any effect. 我尝试了Android Studio中的所有属性,但是它们没有任何作用。 I googled, opened many questions but I still have no luck. 我用谷歌搜索,打开了许多问题,但我仍然没有运气。 What magic property shall I use? 我应该使用什么魔法财产?

As you can see items are positioned on left side of the screen. 如您所见,项目位于屏幕左侧。 I want to move it to center. 我想将其移到中心。 And there is no space between first and second row. 第一和第二行之间没有空间。

在此处输入图片说明 在此处输入图片说明

The easiest solution for (1) is to add top and/or bottom margin, like @suraj said. (1)最简单的解决方案是增加顶部和/或底部边距,如@suraj所说。

Regarding (2) - the critical issue is that your items are square. 关于(2)-关键问题是您的物品是正方形的。 To get it working, set android:layout_width="match_parent" and change the design of your items to use the actual smaller dimension (width or height) for both dimensions (maybe all you need to do in your case is to make the background square). 要使其正常工作,请设置android:layout_width="match_parent"并更改商品的设计,以将实际较小的尺寸(宽度或高度)用于这两个尺寸(也许您需要做的就是使背景变为正方形) )。 Then display the content centered within the item's LinearLayout (you are already doing this). 然后显示位于项目的LinearLayout中心的内容(您已经在执行此操作)。 You have to do it this way because GridLayoutManager does not support layout_gravity at this time. 您必须这样做,因为GridLayoutManager目前不支持layout_gravity。

For 1.there is no space between rows 对于1.行之间没有空格

Try android:layout_marginBottom="10dp" in the linear layout of item.This should provide the spacing below each item. 在项目的线性布局中尝试android:layout_marginBottom="10dp" 。这应在每个项目下方提供间距。

For 2.items are not centered within the row 对于2.项目不在行内居中

Looks like you have given right margin to your recyclerview and no left margin. 看来您已为自己的recyclerview设置了适当的边距,而没有剩余的边距。

android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"

should give equal spacing on both sides. 两侧应等距。 or set 或设置

app:layout_gravity="center"

in linear layout of the item. 在项目的线性布局中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 gridlayoutmanager 在 recyclerview 中显示 9 个项目 - show 9 items in recyclerview with gridlayoutmanager 使用 GridLayoutManager 在 RecyclerView 中拖放项目 - Drag and drop items in RecyclerView with GridLayoutManager 使用 GridLayoutManager 将项目粘贴到 RecyclerView 的底部 - Stick items to bottom inside RecyclerView with GridLayoutManager 如何在使用gridlayoutmanager的recyclerview中水平居中放置项目 - How to center horizontally items in recyclerview that uses gridlayoutmanager GridLayoutManager(RecyclerView)中的项目更改滚动Android上的位置 - items in GridLayoutManager (RecyclerView) changes position on scroll android RecyclerView GridLayoutManager项目随机移动位置 - RecyclerView GridLayoutManager items shifting positions randomly 使用GridLayoutManager实现RecyclerView中的项目 - Centering items in a RecyclerView when it's implemented with GridLayoutManager 在 RecyclerView 中水平居中项目(使用 GridLayoutManager) - center items horizontally in RecyclerView(using GridLayoutManager) 为什么RecyclerView项目在GridLayoutManager中随机移位? - Why RecyclerView items randomly shift postions in GridLayoutManager? 装饰 RecyclerView(使用 GridLayoutManager)以显示项目之间的分隔线 - Decorating RecyclerView (with GridLayoutManager) to display divider between items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM