5

I want to add divider lines in my RecyclerView Layout. I already searched some time, but I can only find solutions that utilise RecyclerView.addItemDecoration, which adds the divider between all items. I could create a layout that has a single line and add that to the RecyclerView, but that doesn't seem like an elegant solution to me.

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
BBotMerlin
  • 230
  • 1
  • 3
  • 12
  • Create your own decorator then you can decide when the divider gets drawn in the onDraw method – Brian Aug 10 '17 at 11:10

5 Answers5

6
DividerItemDecoration myDivider = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);

myDivider.setDrawable(ContextCompat.getDrawable(context, R.drawable.cutm_dvdr));
yourRecyclerView.addItemDecoration(myDivider);

add cutm_dvdr.xml in drawable folder

 <?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="#e20" />
    </shape>
Mohd Saquib
  • 580
  • 4
  • 14
4

You can write a custom RecyclerView.ItemDecoration and draw divider only where you need. And set it to RecyclerView using:

recyclerView.addItemDecoration(new YourItemDecoration());

This is the default DividerItemDecoration source code: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v7/recyclerview/src/android/support/v7/widget/DividerItemDecoration.java

You can find the divider drawing logic in onDraw method, where it draws divider for all items. You have to change that part based on your needs to draw divider for some items only. getItemOffsets() method adds offset to the item to make space for the divider.

Bob
  • 13,447
  • 7
  • 35
  • 45
1

One way is to create a custom ItemDecoration class

// from Bhuvanesh BS - https://stackoverflow.com/questions/46215810/recyclerview-remove-divider-decorator-after-the-last-item
class DividerItemDecorator(private val divider: Drawable?, private val viewIds: IntArray? = null, private val notViewIds: IntArray? = null) : ItemDecoration() {
    override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val dividerLeft = parent.paddingLeft
        val dividerRight = parent.width - parent.paddingRight
        val childCount = parent.childCount
        for (i in 0 until childCount - 1) {
            val child: View = parent.getChildAt(i)
            if (hasDivider(child.id)) {
                divider?.also {
                    val params = child.layoutParams as RecyclerView.LayoutParams
                    val dividerTop: Int = child.bottom + params.bottomMargin
                    val dividerBottom = dividerTop + it.intrinsicHeight
                    it.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom)
                    it.draw(canvas)
                }
            }
        }
    }
    
    private fun hasDivider(viewId: Int): Boolean {
        return when (viewIds != null) {
            true -> viewIds.contains(viewId)
            false -> {
                when (notViewIds != null) {
                    true -> !notViewIds.contains(viewId)
                    false -> true
                }
            }
        }
    }
}

then pass it the view ids of the items that have a divider

recyclerView.addItemDecoration(DividerItemDecorator(ContextCompat.getDrawable(requireContext(), R.drawable.item_divider), viewIds = intArrayOf(R.id.root_view_id_1, R.id.root_view_id_2)))

or the ids that don't have a divider

recyclerView.addItemDecoration(DividerItemDecorator(ContextCompat.getDrawable(requireContext(), R.drawable.item_divider), notViewIds = intArrayOf(R.id.root_view_id_1, R.id.root_view_id_2)))
Eric
  • 2,573
  • 1
  • 23
  • 19
0

you can add custom view in recycler view row item layout and set visibility visible or gone set in adapter which row item you want to line you can set.

-3

DividerItemDecoration recycleViewDivider = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL); recycleViewDivider.setDrawable(ContextCompat.getDrawable(context, R.drawable.recycleview_item_divider));

and add this DividerItemDecoration to you RecycleView RecyclerView.addItemDecoration(recycleViewDivider );

Anand Raj Mehta
  • 437
  • 5
  • 4