Last active
May 5, 2020 20:33
-
-
Save Leaking/3e69ee94a278abe16f10 to your computer and use it in GitHub Desktop.
A simple RecyclerView ItemDecoration drawing a horizon line and you can set the height and the color.It looks like the listview divider.
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
package com.quinn.xmpp.ui.widget; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.drawable.Drawable; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import com.quinn.xmpp.R; | |
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration { | |
private int mColor; | |
private int mHeight; // in px format | |
private Paint paint; | |
public SimpleDividerItemDecoration(Context context, int color, int height) { | |
this.mColor = color; | |
this.mHeight = height; | |
this.paint = new Paint(); | |
paint.setAntiAlias(true); | |
} | |
@Override | |
public void onDrawOver(Canvas c, RecyclerView parent) { | |
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 + mHeight; | |
paint.setColor(mColor); | |
c.drawRect(left, top, right, bottom, paint); | |
} | |
} | |
} |
You'll also want to extend the itemOffset to fit the drawn rectangle, or else you'll be drawing on top of your views: https://gist.github.com/nivekmai/7b9d20629a22f7ada87c849ed9dcbc32
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kotlin version: