How to center items of a recyclerview?

前端 未结 8 1737
夕颜
夕颜 2020-12-14 14:38

I need to center elements in each row so they will be like in this mockup. I\'ve been searching if there is any layout that works that way without look. items are centered i

相关标签:
8条回答
  • 2020-12-14 15:05

    Currently, I think we need to write our own custom view for it because Android has not supported this feature as we expected yet.

    It is the Sample I made in case someone needs it: https://github.com/mttdat/utils

    (Try the CenterGridActivity.kt by adjusting Manifest file to start this activity as default)

    0 讨论(0)
  • 2020-12-14 15:10

    you can use the new layoutManager from google FlexLayoutManager it will gives you a lot of control and flexibility over the recyclerView items

    0 讨论(0)
  • 2020-12-14 15:12

    Use FlexboxLayout from com.google.android:flexbox library. Note the flexwrap and justifyContent property values. Then, set layout_wrapBefore = true on the view that you want to wrap the line of items before.

        <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>
    
    0 讨论(0)
  • 2020-12-14 15:13

    Make recyclerview width to wrap_content and its container's layout_gravity to 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>
    
    0 讨论(0)
  • 2020-12-14 15:15

    Use the gridLayoutManager = new GridLayoutManager(context,6) and override setSpanSizeLookup.

    Example:

    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;
                            }
                        }
                    });
    

    This will work if you want to display 3 items in a row and remaining in the center of the grid.

    Change the grid layout manager span counts accordingly your requirement.

    0 讨论(0)
  • 2020-12-14 15:16

    I am assuming that you are using a LinearLayoutManager with a RecyclerView for a ListView-style effect. In that case, use a horizontal LinearLayout for each row, with android:gravity="center" to center its contents.

    0 讨论(0)
提交回复
热议问题