How to add dividers and spaces between items in RecyclerView?

前端 未结 30 2814
清歌不尽
清歌不尽 2020-11-22 03:27

This is an example of how it could have been done previously in the ListView class, using the divider and dividerHeight parame

相关标签:
30条回答
  • 2020-11-22 04:25

    The way how I'm handling the Divider view and also Divider Insets is by adding a RecyclerView extension.

    1.

    Add a new extension file by naming View or RecyclerView:

    RecyclerViewExtension.kt
    

    and add the setDivider extension method inside the RecyclerViewExtension.kt file.

    /*
    * RecyclerViewExtension.kt
    * */
    import androidx.annotation.DrawableRes
    import androidx.core.content.ContextCompat
    import androidx.recyclerview.widget.DividerItemDecoration
    import androidx.recyclerview.widget.RecyclerView
    
    
    fun RecyclerView.setDivider(@DrawableRes drawableRes: Int) {
        val divider = DividerItemDecoration(
            this.context,
            DividerItemDecoration.VERTICAL
        )
        val drawable = ContextCompat.getDrawable(
            this.context,
            drawableRes
        )
        drawable?.let {
            divider.setDrawable(it)
            addItemDecoration(divider)
        }
    }
    

    2.

    Create a Drawable resource file inside of drawable package like recycler_view_divider.xml:

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetLeft="10dp"
        android:insetRight="10dp">
    
        <shape>
            <size android:height="0.5dp" />
            <solid android:color="@android:color/darker_gray" />
        </shape>
    
    </inset>
    

    where you can specify the left and right margin on android:insetLeft and android:insetRight.

    3.

    On your Activity or Fragment where the RecyclerView is initialized, you can set the custom drawable by calling:

    recyclerView.setDivider(R.drawable.recycler_view_divider)
    

    4.

    Cheers

    0 讨论(0)
  • 2020-11-22 04:25

    Instead of create a shape xml for change the divider height and color. You can create programmatically like

    val divider = DividerItemDecoration(context,
            DividerItemDecoration.VERTICAL)
    
    divider.setDrawable(ShapeDrawable().apply {
        intrinsicHeight = resources.getDimensionPixelOffset(R.dimen.dp_15)
        paint.color = Color.RED // note: currently (support version 28.0.0), we can not use tranparent color here, if we use transparent, we still see a small divider line. So if we want to display transparent space, we can set color = background color or we can create a custom ItemDecoration instead of DividerItemDecoration. 
    })
    
    recycler_devices.addItemDecoration(divider)
    
    0 讨论(0)
  • 2020-11-22 04:27

    This is simple, you don't need such complicated code

    DividerItemDecoration divider = new 
    DividerItemDecoration(mRVMovieReview.getContext(), 
    DividerItemDecoration.VERTICAL);
    divider.setDrawable(
        ContextCompat.getDrawable(getBaseContext(), R.drawable.line_divider)
    );
    mRVMovieReview.addItemDecoration(divider);
    

    Add this in your drawable : line_divider.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle">
        <size android:height="1dp" />
        <solid android:color="@android:color/black" />
    </shape>
    
    0 讨论(0)
  • 2020-11-22 04:27

    OCTOBER 2016 UPDATE

    With support library v25.0.0 there finally is a default implementation of basic horizontal and vertical dividers available!

    https://developer.android.com/reference/android/support/v7/widget/DividerItemDecoration.html

    0 讨论(0)
  • 2020-11-22 04:28

    Since there is no right way to implement this yet properly using Material Design, I just did the following trick to add a divider on the list item directly:

    <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/dividerColor"/>
    
    0 讨论(0)
  • 2020-11-22 04:29

    Too Late but for GridLayoutManager I use this:

    public class GridSpacesItemDecoration : RecyclerView.ItemDecoration
    {
        private int space;
    
        public GridSpacesItemDecoration(int space) {
            this.space = space;
        }
    
        public override void GetItemOffsets(Android.Graphics.Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
        {
            var position = parent.GetChildLayoutPosition(view);
    
            /// Only for GridLayoutManager Layouts
            var manager = parent.GetLayoutManager() as GridLayoutManager;
    
            if (parent.GetChildLayoutPosition(view) < manager.SpanCount)
                outRect.Top = space;
    
            if (position % 2 != 0) {
                outRect.Right = space;
            }
    
            outRect.Left = space;
            outRect.Bottom = space;
        }
    }
    

    This work for any span count you have.

    Ollie.

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