Add margins to divider in RecyclerView

后端 未结 8 882
深忆病人
深忆病人 2020-12-14 01:11

i am building an android app which is using RecyclerView. I want to add dividers to RecyclerView, which I did using this code:

Divi         


        
相关标签:
8条回答
  • 2020-12-14 01:24

    You can create your own item decoration for recycler view. Here is code for the same.

    public class SimpleItemDecorator extends RecyclerView.ItemDecoration {
    
        int space;
        boolean isHorizontalLayout;
        public SimpleItemDecorator(int space) {
            this.space = space;
        }
    
        public SimpleItemDecorator(int space, boolean isHorizontalLayout) {
            this.space = space;
            this.isHorizontalLayout = isHorizontalLayout;
        }
    
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            if(isHorizontalLayout)
            {
                outRect.bottom=space;
                outRect.right=space;
                outRect.left=space;
                outRect.top=space;
    
            } else {
                outRect.bottom = space;
                if (parent.getChildAdapterPosition(view) == 0)
                    outRect.top = space;
                else
                    outRect.top = 0;
    
            }
    
    
        }
    }
    

    And to use it with your recyclerview you can do like this:

     recyclerView.addItemDecoration(new SimpleItemDecorator(5));
    
    0 讨论(0)
  • 2020-12-14 01:25

    Use this and customize according to your requirement.

    public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    
        private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    
        private Drawable divider;
    
        /**
         * Default divider will be used
         */
        public DividerItemDecoration(Context context) {
            final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
            divider = styledAttributes.getDrawable(0);
            styledAttributes.recycle();
        }
    
        /**
         * Custom divider will be used
         */
        public DividerItemDecoration(Context context, int resId) {
            divider = ContextCompat.getDrawable(context, resId);
        }
    
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
    
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
    
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
    
                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + divider.getIntrinsicHeight();
    
                divider.setBounds(left, top, right, bottom);
                divider.draw(c);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-14 01:27

    Below is the code for divider in RecyclerView with left margin. Just paste the code in your OnCreate method of MainActivity.

    class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
        private Drawable mDivider;
    
        public SimpleDividerItemDecoration(Context context) {
            mDivider = context.getResources().getDrawable(R.drawable.divider);
        }
    
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            int left = 250;
            int right = parent.getWidth() - parent.getPaddingRight();
    
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
    
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
    
                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDivider.getIntrinsicHeight();
    
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }
    
    recyclerView.addItemDecoration(new SimpleDividerItemDecoration(
                getApplicationContext()));
    

    Since, the divider.xml file will be missing from your drawable folder so below is the code that you have to paste on the divider layout after creating it on drawable folder.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
      <size
        android:width="1dp"
        android:height="1dp" />
    
      <solid android:color="@color/divider" />
    
    </shape>
    
    0 讨论(0)
  • 2020-12-14 01:28

    shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#E6E7F0" />
        <size android:height="1dp" />
    </shape>
    

    layer.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/shape"
            android:left="16dp"
            android:right="16dp" />
    
    </layer-list>
    

    Code

    val itemDecoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
    itemDecoration.setDrawable(resources.getDrawable(R.drawable.layer, null))
    recyclerView.addItemDecoration(itemDecoration)
    
    0 讨论(0)
  • 2020-12-14 01:31

    Here is a simple Kotlin code snippet to implement ItemDecoration with RecyclerView:

     recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                        outRect.left = 20    // Left Margin.
                        outRect.right = 20   // Right Margin.
                        outRect.top = 16     // Top Margin.
                        outRect.bottom = 16  // Bottom Margin.
                    }
                })
    

    Explantation:- In the above example code, we are adding margins to each item of RecyclerView from all four directions.

    Happy Coding...

    0 讨论(0)
  • 2020-12-14 01:34

    Elaborating on @SeptimusX75: if you (like me) prefer to do UI stuff in XML you can create an inset drawable file. You'll have to create a second XML file but since in return your code gets cleaner I say it's worth it :-).

    divider_base.xml (the actual divider):

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <size android:height="1dp" />
        <solid android:color="@color/dividerColor" />
    </shape>
    

    divider.xml (the inset):

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/divider_base"
        android:insetRight="20dp"
        android:insetLeft="20dp">
    </inset>
    
    0 讨论(0)
提交回复
热议问题