Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public DividerItemDecoration(Context context, AttributeSet attrs) {
final TypedArray a = context.obtainStyledAttributes(attrs, new int [] { android.R.attr.listDivider });
mDivider = a.getDrawable(0);
a.recycle();
}
public DividerItemDecoration(Drawable divider) { mDivider = divider; }
@Override
public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (mDivider == null) return;
if (parent.getChildPosition(view) < 1) return;
if (getOrientation(parent) == LinearLayoutManager.VERTICAL) outRect.top = mDivider.getIntrinsicHeight();
else outRect.left = mDivider.getIntrinsicWidth();
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent) {
if (mDivider == null) { super.onDrawOver(c, parent); return; }
if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i=1; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int size = mDivider.getIntrinsicHeight();
final int top = child.getTop() - params.topMargin;
final int bottom = top + size;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
} else { //horizontal
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i=1; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int size = mDivider.getIntrinsicWidth();
final int left = child.getLeft() - params.leftMargin;
final int right = left + size;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
private int getOrientation(RecyclerView parent) {
if (parent.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
return layoutManager.getOrientation();
} else throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager.");
}
}
@mheras

This comment has been minimized.

Copy link

mheras commented Nov 7, 2014

Hi! How do I use this?

@pnickv

This comment has been minimized.

Copy link

pnickv commented Nov 13, 2014

something like
...
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.abc_list_divider_mtrl_alpha)));

@cbedoy

This comment has been minimized.

Copy link

cbedoy commented Nov 21, 2014

is it most easy if declare a xml with margin top and margin bottom?

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 2, 2014

This si also deprecated now, onDrawOver should now also take in RecyclerView.State

@lapastillaroja

This comment has been minimized.

Copy link

lapastillaroja commented Dec 10, 2014

@fatfingers
Thanks for sharing this git.
I forked and modified your DividerItemDecoration. I hope can be useful to somebody.

https://gist.github.com/lapastillaroja/858caf1a82791b6c1a36

@franciscossb

This comment has been minimized.

Copy link

franciscossb commented Dec 11, 2014

Great work, but how can I set the divider height?

Thank you

@svenoaks

This comment has been minimized.

Copy link

svenoaks commented Jan 11, 2015

Why do we now need 75 lines of code to draw a list divider?

@aNinjaMonk

This comment has been minimized.

Copy link

aNinjaMonk commented Jan 20, 2015

Because GOOGLE assumes android developers are not humans ...... but super humans... who for example if asked for time will first invent a clock and then a time machine to go back into the moment you asked for time and then tell you the time... developing for iOS is much more human...

@atabouraya

This comment has been minimized.

Copy link

atabouraya commented Jan 22, 2015

@cbedoy you still have a problem with the first and the last items, usually you don't need margin top for the first item and margin bottom for the last one

@MrThiago

This comment has been minimized.

Copy link

MrThiago commented Jul 21, 2015

Hi, I tried it. but I get an error, Call require API level 21

mListView.addItemDecoration(
new DividerItemDecoration(getActivity().getDrawable(R.drawable.ic_launcher), true, true));

@aricneto

This comment has been minimized.

Copy link

aricneto commented Jul 26, 2015

@MrThiago, getDrawable(int, theme) is for API 21+ only. Use

ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_launcher)
@mudao

This comment has been minimized.

Copy link

mudao commented Aug 9, 2015

this only for LinearLayoutManager ? How can I draw dividers for StaggeredGridManager or GridLayoutManager?

@pratikbutani

This comment has been minimized.

Copy link

pratikbutani commented Oct 2, 2015

getChildPosition and onDrawOver deprecated. What should i do?

@ChaosLeung

This comment has been minimized.

Copy link

ChaosLeung commented Nov 18, 2015

@pratikbutani
use getChildAdapterPosition or getChildLayoutPosition instead getChildPosition

use

public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)

instead

public void onDrawOver(Canvas c, RecyclerView parent)
@seyoung-hyun

This comment has been minimized.

Copy link

seyoung-hyun commented Jun 21, 2016

@fatfingers
Hi, I'm Seyoung.
I want to use your code in my commercial app.
But I can'f find any terms and conditions for use, reproduction, and distribution.
I wonder if I can use the code for commercial distribution or not.
Please let me know how I can use the code for commercial distribution.
Thanks.

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.