如何集中回收者视图的项目?

时间:2015-05-02 22:00:36

标签: android android-layout android-recyclerview

我需要将每行中的元素居中,这样它们就像在这个模型中一样。 我一直在寻找是否有任何布局以这种方式工作而不看。 项目以行为中心。

mockup

现在我的代码就是它的样子。enter image description here

9 个答案:

答案 0 :(得分:37)

将recyclerview width设为wrap_content及其容器layout_gravitycenter_horizontal

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

      <android.support.v7.widget.RecyclerView
           android:id="@+id/recycrer_view"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
           android:paddingLeft="16dp"
           android:paddingRight="16dp" />
 </LinearLayout>

答案 1 :(得分:31)

LinearLayout项目行布局中取RecyclerView,然后将android:layout_gravity="center"提交给LinearLayout

对于每一行图像,您必须采用不同的LinearLayout

以下是示例代码:

 <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:orientation="horizontal">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image1" />

            <ImageView
                android:id="@+id/image2"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image2" />

           <ImageView
                android:id="@+id/image3"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:src="@drawable/image3" />

  </LinearLayout>

答案 2 :(得分:8)

我假设您使用LinearLayoutManager RecyclerView作为ListView式效果。在这种情况下,请为每行使用horizontal LinearLayoutandroid:gravity="center"将其内容置于中心位置。

答案 3 :(得分:5)

实现:

recycler.adapter = MyAdapter()
val layoutManager FlexboxLayoutManager( context: this)
layoutManager.justifyContent = JustifyContent.CENTER
layoutManager.alignItems = AlignItems.CENTER
layoutManager.flexDirection = FlexDirection. ROW layoutManager.flexWrap = FlexWrap.WRAP
my recycler.layout Manager = = layout Manager

enter image description here

答案 4 :(得分:3)

您可以动态添加一些布局,例如:

  • 适配器对象可以是水平LinearLayout

1-在java代码中创建一个LinearLayout并自定义它(gravity,...)

2-将一些图标添加到linearLayout

3-将linearLayout添加到您的适配器

4-重复1,2,3

    // : declare new horizontal linearLayout
    ImageView myIcons[nomOfIcons];
    // : add all icons to myIcons
    for(int i=1; i<=nomOfIcons; i++){
        linearLayout.addView(myIcons[i - 1]);
        if(i%numOfIconsInOneHorizontalLinearLayout==0) {
            results.add(linearLayout); // add linearLayout to adapter dataSet
            // : declare new horizontal linearLayout
        }
    }
    if(n%numOfIconsInOneHorizontalLinearLayout!=0) // add last linearLayout if not added in the loop
        results.add(linearLayout);
    mAdapter.notifyDataSetChanged(); // update adapter

答案 5 :(得分:1)

使用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个项目并保留在网格的中心,这将有效。

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

答案 6 :(得分:1)

您可以使用google FlexLayoutManager中的新layoutManager 它会为recyclerView项目提供很多控制和灵活性

答案 7 :(得分:1)

使用FlexboxLayout库中的com.google.android:flexbox。请注意flexwrapjustifyContent属性值。然后,在您要先包装项目行的视图上设置layout_wrapBefore = true

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:flexWrap="wrap"
        app:justifyContent="center">

        <View ... />

        <View app:layout_wrapBefore="true" ... />

        <View ... />

    </com.google.android.flexbox.FlexboxLayout>

答案 8 :(得分:0)

当前,我认为我们需要为其编写自己的自定义视图,因为Android尚未像我们期望的那样支持此功能。

这是我制作的样本,以防有人需要: https://github.com/mttdat/utils

(尝试通过调整清单文件来尝试启动CenterGridActivity.kt,以默认启动此活动)