i am building an android app which is using RecyclerView
. I want to add dividers to RecyclerView
, which I did using this code:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation()); recyclerView.addItemDecoration(dividerItemDecoration);
So far everything works fine. However, the divider is taking the size of full screen and I want to add margins to it. Is there any way that I can add margins to the divider using a method that will add some space to the rectangle drawn and not by creating a custom drawable shape with margins and add it to the RecyclerView
?
DividerItemDecoration is a RecyclerView. ItemDecoration that can be used as a divider between items of a LinearLayoutManager . It supports both HORIZONTAL and VERTICAL orientations.
Try to use cardElevation=0dp. This should remove the extra spacing between recyclerview items.
I think the most straightforward solution is to use the setDrawable method on the Decoration object and pass it an inset drawable with the inset values you want for the margins. Like so:
int[] ATTRS = new int[]{android.R.attr.listDivider}; TypedArray a = context.obtainStyledAttributes(ATTRS); Drawable divider = a.getDrawable(0); int inset = getResources().getDimensionPixelSize(R.dimen.your_margin_value); InsetDrawable insetDivider = new InsetDrawable(divider, inset, 0, inset, 0); a.recycle(); DividerItemDecoration itemDecoration = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL); itemDecoration.setDrawable(insetDivider); recyclerView.addItemDecoration(itemDecoration);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With