【问题标题】:How to center items of a recyclerview?如何将recyclerview的项目居中?
【发布时间】:2015-05-02 22:00:35
【问题描述】:

我需要将每一行中的元素居中,这样它们就会像这个模型中一样。 我一直在寻找是否有任何布局可以在没有外观的情况下以这种方式工作。 项目在其行中居中。

这就是它现在在我的代码中的样子。

【问题讨论】:

  • 这个问题解决了吗?
  • 您可能只需要制作一个自定义 layoutManager,因为 GridLayoutManager 对此没有帮助。我确实发布了答案,但我不确定它是否正是您想要的。
  • 这个布局怎么做?

标签: android android-layout android-recyclerview


【解决方案1】:

将 recyclerview width 设置为 wrap_content 并将其容器的 layout_gravity 设置为 center_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>

【讨论】:

  • 这将防止从侧面滚动 RecyclerView。
【解决方案2】:

RecyclerView 的项目行布局中使用LinearLayout,然后将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>

【讨论】:

  • 我不得不修改 RecyclerView layout_width="wrap_content" 而不是 match_parent
  • @jemkuan 这将在用户在父级的边缘或外部区域滑动时禁用滚动,因为 RecyclerView 的宽度将小于父级的宽度。
  • 这种方法的问题是,当用户触摸图像视图之外时,回收器视图将注册一个点击事件。
【解决方案3】:

达到:

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

【讨论】:

    【解决方案4】:

    我假设您使用LinearLayoutManagerRecyclerView 来获得ListView 风格的效果。在这种情况下,对每一行使用horizontalLinearLayout,并使用android:gravity="center" 使其内容居中。

    【讨论】:

    • 其实我用的是gridlayout,不过让我试试吧。
    • @cgomezmendez:我认为通过GridLayoutManager 或通过自定义RecyclerView.LayoutManager 可以做到这一点。一眼看不到如何使用GridLayoutManager
    • 问题解决了吗?
    • 对于我的项目,使用带有网格布局的回收器视图,其中项目使用重力属性居中工作正常。
    • 这没有帮助:(
    【解决方案5】:

    使用来自com.google.android:flexbox 库的FlexboxLayout。请注意 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>
    

    【讨论】:

    • 我想补充一下,这是一种非常干净的方法。特别是您可以将库用作layoutManagerrecyclerview。您所要做的就是设置具有方向和对齐要求的布局管理器。无需制作第三个最佳答案提到的二维回收站视图。
    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      您可以使用来自 google FlexLayoutManager 的新 layoutManager 它将为您提供对 recyclerView 项目的大量控制和灵活性

      【讨论】:

        【解决方案8】:

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

        • 适配器对象可以是水平线性布局

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

        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
        

        【讨论】:

          【解决方案9】:

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

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

          (通过调整 Manifest 文件尝试使用 CenterGridActivity.kt 以默认启动此活动)

          【讨论】:

            【解决方案10】:

            相信我,这会奏效。 在放置回收站视图的主 xml 文件中,复制我的代码。

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_50sdp"
                    android:gravity="center">
            
                    <androidx.recyclerview.widget.RecyclerView
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/_50sdp"/>
            
                </LinearLayout>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-10-24
              • 2016-02-19
              • 2016-12-02
              • 2021-01-06
              相关资源
              最近更新 更多