Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple RecyclerView Divider

Simple RecyclerView Divider

Simple Horizontal Divider Item Decoration for RecyclerView

    mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(
            getApplicationContext()
    	));

NOTE: Add item decoration prior to setting the adapter

<?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/dark_gray" />
</shape>
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public SimpleDividerItemDecoration(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.line_divider);
}
@Override
public void onDrawOver(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 + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
@AllenDang

This comment has been minimized.

Copy link

commented Jan 8, 2015

I cannot manage to make it work, I add the decoration like this:

    lvCanvas.setLayoutManager(lm);
    lvCanvas.addItemDecoration(new SimpleDividerItemDecoration(this));
    lvCanvas.setAdapter(mChapterContentAdapter);
@jahk88

This comment has been minimized.

Copy link

commented Jan 13, 2015

Hi, I had the same problem and I solved it overriding the onDrawOver method instead of the onDraw.
Hope It helps =D

@fazlurr

This comment has been minimized.

Copy link

commented Feb 6, 2015

Works like a charm, Thanks!

@AmauryEsparza

This comment has been minimized.

Copy link

commented Feb 9, 2015

It works, thanks

@Solid-Color-Labs

This comment has been minimized.

Copy link

commented Mar 10, 2015

Is there any way to make this work with a grid layout?

@bohbra

This comment has been minimized.

Copy link

commented Mar 13, 2015

I notice the divider is being drawn on top of the scrollbar, anyway around this?

@Leaking

This comment has been minimized.

Copy link

commented Mar 17, 2015

@keyoh
the onDrawover() method will draw the divider on the top of the item view。as a result, it also draws on the top of the scrollbar

@PsyGik

This comment has been minimized.

Copy link

commented Apr 22, 2015

The proposed code above adds a divider to every item including the last item.

So one can even use

<View 
  android:id="@+id/divider"
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="@color/holo_gray_light" />

in the layout for the item to get the same effect.

As a workaround, in the adapter I check if the child is the last item and change the visibility of the divider accordingly.

 @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
       if(position==getItemCount()) //check if this is the last child, if yes then hide the divider
         viewHolder.divider.setVisibility(View.GONE);
    }

Any suggestions on this are welcome

@elikohen

This comment has been minimized.

Copy link

commented Apr 26, 2015

It works like a charm! I wanted to use only the onDraw method so I added this other override to add space for the drawable:

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.bottom = mDivider.getIntrinsicHeight();
}

I hope it helps someone. Thanks!

@willblaschko

This comment has been minimized.

Copy link

commented May 21, 2015

@PsyGik

A couple quick comments: because the views are recycled, you'll want to make it visible if it's not the last item. You'll also never trigger this because position will never equal getItemCount() (as long as you're returning something like List.size()). Otherwise looks like a solid alternative that'll work with ListViews too :)

Here's a quick update to your code:

int visible = (position == getItemCount()-1) ? View.GONE : View.VISIBLE;
holder.divider.setVisibility(visible);
@jonasbleyl

This comment has been minimized.

Copy link

commented Aug 10, 2015

Thanks, works well.
Though getDrawable(int) is deprecated now. Use this instead:

ContextCompat.getDrawable(context, R.drawable.line_divider);
@erwinjnefer

This comment has been minimized.

Copy link

commented Aug 26, 2015

Thank's so much... it's work ....

@bangiki

This comment has been minimized.

Copy link

commented Oct 6, 2015

@jonasbleyl : where is ContextCompat declaration?

@chrisvoronin

This comment has been minimized.

Copy link

commented Nov 5, 2015

ContextCompat is in the support library. ContextCompat.getDrawable(context, resourceID)

@yosisah

This comment has been minimized.

Copy link

commented Nov 10, 2015

Thanks, works great.

@krupalshah

This comment has been minimized.

Copy link

commented Nov 14, 2015

thanks

@Gnzlt

This comment has been minimized.

Copy link

commented Jan 4, 2016

Nice Work!
I've also included the default Android listDivider drawable inside the constructor if you don't want to implement a custom one:

public SimpleDividerItemDecoration(Context context) {
    final TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.listDivider});
    mDivider = a.getDrawable(0);
    a.recycle();
}
@aantthony

This comment has been minimized.

Copy link

commented Apr 23, 2016

Thanks.
I used this:

ResourcesCompat.getDrawable(context.getResources(), R.drawable.line_divider, context.getTheme());
@Rupali1992

This comment has been minimized.

Copy link

commented Apr 29, 2016

I am not able to set divider to my RecyclerView can somebody help me for this?......

ResourcesCompat.getDrawable(context.getResources(), R.drawable.line_divider, context.getTheme());

where should i used this above code?please specify the location

@jovisalonga

This comment has been minimized.

Copy link

commented Jul 20, 2016

Tried many, but this works like magic.

Thanks

@ParisaRashidi

This comment has been minimized.

Copy link

commented Sep 16, 2016

it works
thanks alot

@jfrere

This comment has been minimized.

Copy link

commented Sep 26, 2016

Works perfectly. Thank you

@okadaNana

This comment has been minimized.

Copy link

commented Oct 31, 2016

Good

@zhonglushu

This comment has been minimized.

Copy link

commented Jan 25, 2017

I think custom drawable is not work

@axellebot

This comment has been minimized.

Copy link

commented Mar 7, 2017

Hello,
Maybe use
ContextCompat

public SimpleDividerItemDecoration(Context context) { mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider); }

@Gematlive

This comment has been minimized.

Copy link

commented May 10, 2017

Thanks a lot

@archigoel

This comment has been minimized.

Copy link

commented May 17, 2017

Thanks,It worked!!

@androidqasim

This comment has been minimized.

Copy link

commented Feb 12, 2018

593835000c9841a6b0f2d8d9d0bb6ec9
how can i made this?

@paulomcnally

This comment has been minimized.

Copy link

commented Feb 16, 2018

https://developer.android.com/reference/android/support/v7/widget/DividerItemDecoration.html

val linearLayoutManager = LinearLayoutManager(this)
val dividerItemDecoration = DividerItemDecoration(recyclerView.context, linearLayoutManager.orientation)

// Init recyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = linearLayoutManager
recyclerView.addItemDecoration(dividerItemDecoration)
@Geet-Thakur01

This comment has been minimized.

Copy link

commented May 9, 2018

Use "card view" for item layout and set "margin top" according to your requirement.
:)

@jkhin

This comment has been minimized.

Copy link

commented Sep 4, 2018

It works beauty... Thanks!

@VSG24

This comment has been minimized.

Copy link

commented Jan 9, 2019

If you don't want the last item to have a divider line after it, simply change for (int i = 0; i < childCount; i++) { to for (int i = 0; i < childCount - 1; i++) { and it will skip the last item divider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.