Last active
October 9, 2016 14:17
-
-
Save talosdev/07747f1281aeee1f317663d11e7a0f96 to your computer and use it in GitHub Desktop.
RecyclerView dividers with ItemDecoration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Drawable divider = ContextCompat.getDrawable(context, R.drawable.last_events_divider); | |
previousEventsRecycler.addItemDecoration(new RecyclerView.ItemDecoration() { | |
/** | |
* Draws a divider (1dp-line) inside (over) the child. | |
* Applies to all elements except the first one. | |
* @param c | |
* @param parent | |
* @param state | |
*/ | |
@Override | |
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
super.onDrawOver(c, parent, state); | |
int dividerLeft = parent.getPaddingLeft(); | |
int dividerRight = parent.getWidth() - parent.getPaddingRight(); | |
int childCount = parent.getChildCount(); | |
for (int i = 1; i < childCount; i++) { | |
View child = parent.getChildAt(i); | |
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); | |
int dividerTop = child.getTop() - params.topMargin; | |
int dividerBottom = dividerTop + divider.getIntrinsicHeight(); | |
divider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom); | |
divider.draw(c); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="@color/isabelline" /> | |
</shape> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment