Skip to content

Instantly share code, notes, and snippets.

@polbins
Last active February 29, 2024 09:58
Show Gist options
  • Save polbins/e37206fbc444207c0e92 to your computer and use it in GitHub Desktop.
Save polbins/e37206fbc444207c0e92 to your computer and use it in GitHub Desktop.
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);
}
}
}
@jovisalonga
Copy link

Tried many, but this works like magic.

Thanks

@ParisaRashidi
Copy link

it works
thanks alot

@jayhack7
Copy link

Works perfectly. Thank you

@okadaNana
Copy link

Good

@zhonglushu
Copy link

I think custom drawable is not work

@axellebot
Copy link

axellebot commented Mar 7, 2017

Hello,
Maybe use
ContextCompat

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

@Gematlive
Copy link

Thanks a lot

@archigoel
Copy link

Thanks,It worked!!

@androidqasim
Copy link

593835000c9841a6b0f2d8d9d0bb6ec9
how can i made this?

@paulomcnally
Copy link

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
Copy link

Geet-Thakur01 commented May 9, 2018

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

@jkhin
Copy link

jkhin commented Sep 4, 2018

It works beauty... Thanks!

@vsg24
Copy link

vsg24 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