在 RecyclerView 中水平居中项目(使用 GridLayoutManager) [英] center items horizontally in RecyclerView(using GridLayoutManager)

查看:61
本文介绍了在 RecyclerView 中水平居中项目(使用 GridLayoutManager)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 RecyclerView

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

I tried many things: RelativeLayout as a parent, then use layout_centerInParent, use the basic android:layout_gravity="center", try to add some space element,using weights and .... in item_row.xml
but I didn't succeed :(

我有什么

我想要的

活动中

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>

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

i googled a lot and found this answer but actually i don't know how to create a LinearLayout for each row of items!

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

  • is that the best solution?

还有其他想法吗?

推荐答案

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

you can use a SpanSizeLookup to make views/items with different widths for your grid view. So with that you can do some cleverness with empty views to adjust accordingly. Might be more complicate or simpler depending on how variable your layout actually is.

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

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

for this specific case you show on your drawing you would do something like:

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); 只是为了占用额外的空间

and with that you'll have to make on your adapter, the position 3 and 6 to be an empty/invisible element. Something like new View(context); just to occupy that extra space

这篇关于在 RecyclerView 中水平居中项目(使用 GridLayoutManager)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆