【问题标题】:center items horizontally in RecyclerView(using GridLayoutManager)在 RecyclerView 中水平居中项目(使用 GridLayoutManager)
【发布时间】:2016-05-28 16:52:29
【问题描述】:

我正在尝试将 RecyclerView 中每一行的所有项目水平居中

我尝试了很多东西:RelativeLayout 作为父级,然后使用 layout_centerInParent,使用基本的 android:layout_gravity="center",尝试添加一些空间元素,使用权重和 .... item_row.xml
但我没有成功:(

我有什么

我想要什么

活动中

recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

item_row.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">

<!--   Recycler View Item Row   -->
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/imgThumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="item name"
            android:id="@+id/txtName"
            android:layout_margin="5dp"
            android:singleLine="false"
            android:lines="2"
            android:gravity="center" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:id="@+id/txtCategory"
                android:textStyle="italic"
                android:textColor="#496fa9"
                android:text="subtitle"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:layout_gravity="center" />

            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="48dp"
                android:layout_height="wrap_content"
                android:background="@android:color/black"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:padding="8dp"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

</android.support.v7.widget.CardView>

我用谷歌搜索了很多,发现了这个answer,但实际上我不知道如何为每一行项目创建一个LinearLayout!

  • 这是最好的解决方案吗?

  • 还有其他想法吗?

【问题讨论】:

  • 希望实现非常相似的目标,您有没有设法完成这项工作?
  • @hibob no,看看下面答案中的cmets
  • @AliSherafat 你找到解决方案了吗?
  • @MuhammadAl-kady 哦,不是这样!

标签: android android-layout android-recyclerview gridlayoutmanager


【解决方案1】:

您可以使用 SpanSizeLookup 为您的网格视图制作具有不同宽度的视图/项目。因此,您可以对空视图进行一些巧妙的调整以进行相应的调整。可能更复杂或更简单,具体取决于您的布局的实际可变性。

参考:https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.SpanSizeLookup.html

对于您在绘图上显示的这种特定情况,您可以执行以下操作:

GridLayoutManager lm = new GridLayoutManager(context, 6);
lm.setSpanSizeLookup(new MySizeLookup());

public static class MySizeLookup extends SpanSizeLookup {

   public int getSpanSize(int position) {
      if(position >= 0 && position <= 2) {
          return 2;
      } else if (position == 3 || position == 6) {
          return 1;
      } else {
          return 2;
      }
   }
}

你必须在你的适配器上制作,位置 3 和 6 是一个空/不可见的元素。 new View(context); 之类的东西只是为了占用额外的空间

【讨论】:

  • 我做了很多测试并尝试了您的代码,但没有成功,我认为我应该检查少于 3 个项目的行并将该行的项目放在 LinearLayout 中android:layout_gravity="center" 属性但是我不知道该怎么做
  • @RahulKhurana 答案适用于 3 行,这一行不行。
  • 我有同样的问题,我无法在中心设置行 使用网格布局管理器,我已经尝试过,但没有为每行处理 4 或 5 个项目。有人知道吗?跨度>
  • @civanimahida 你有没有找到任何解决方案来解决 5 个项目的中心行
【解决方案2】:

使用gridLayoutManager = new GridLayoutManager(context,6) 并覆盖setSpanSizeLookup。

例子:

int totalSize=list.size();
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        int span;
                        span = totalSize % 3;
                        if (totalSize < 3) {
                            return 6;
                        } else if (span == 0 || (position <= ((totalSize - 1) - span))) {
                            return 2;
                        } else if (span == 1) {
                            return 6;
                        } else {
                            return 3;
                        }
                    }
                });

如果您想连续显示 3 个项目并保持在网格的中心,这将起作用。

根据您的要求更改网格布局管理器跨度计数。

【讨论】:

  • 嗨@Rahul Devanavar,如何连续处理 4 个项目
【解决方案3】:
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 6);
final int totalSize = customList.size();

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        int extra;
        extra = totalSize % 3;

        if(extra == 0)
            return 2;

        if (totalSize - (position + 1) < extra) {
            return (int) 6 / extra;
        }

        return 2;
    }
});

recycler.setLayoutManager(layoutManager);

【讨论】:

  • 不鼓励仅使用代码回答。请添加一些解释,说明这是如何解决问题的,或者这与现有答案有何不同。 From Review
  • 我可以解释一下,因为我想出了一个类似的解决方案,你必须先熟悉 span 大小,然后注意数字 6,这就是,对于 3 列,我们将使用 6 span 区域,在本例中,3 个项目(3 列)默认情况下每列将占用 2-span 区域。变量extra是剩余项,底部的2,最后的2,要对齐的,这就是为什么 {if extra==0, return 2} 中间的IF状态:当前位置是最后一个- 将要居中的项目?然后分配剩余项目之间的总面积 (6) {return (int) 6 / extra} Saludos
【解决方案4】:
    GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 6);
    final int totalSize = customList.size();

    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int rows = Math.round((totalSize / 3f) + 0.4f);
            int itemInRow = Math.round(((position + 1) / 3f) + 0.4f);
            if (rows == 1 || rows == itemInRow) {
                int span = totalSize % 3;
                if (span == 0) {
                    return 2;
                }
                return 6 / span;
            }
            return 2;
        }
    });

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 2016-02-19
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多