I have a recyclerView
with GridLayoutManager
i want to remove last divider in vertical and horizontal mode.
I tried to set empty to last position in outRect
but it does not work for me.
outRect.setEmpty()
I have an extension
fun
like this :
fun RecyclerView.addItemDecorationWithoutLastDivider(orientation:Int) {
addItemDecoration(object :
DividerItemDecoration(context,orientation) {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) == state.itemCount - 1)
outRect.setEmpty()
else
super.getItemOffsets(outRect, view, parent, state)
}
})
}
I set up recyclerView
here :
recyclerView.apply {
adapter = mBankListAdapter
layoutManager = GridLayoutManager(requireContext(),3,RecyclerView.VERTICAL,false)
addItemDecorationWithoutLastDivider(DividerItemDecoration.VERTICAL)
addItemDecorationWithoutLastDivider(DividerItemDecoration.HORIZONTAL)
}