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
?
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:
Same like @Vivek answer but in Kotlin and different params
You can create your own item decoration for recycler view. Here is code for the same.
And to use it with your recyclerview you can do like this:
Use this and customize according to your requirement.
}