Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thenickreynolds/760d905781ccf20704bd63b92d64c208 to your computer and use it in GitHub Desktop.
Save thenickreynolds/760d905781ccf20704bd63b92d64c208 to your computer and use it in GitHub Desktop.
Add a background color (or in the future dotted outline, etc.) for a cell background that does not move with dragging/swiping
/**
* This adds a background color to all recycler view cells that is not moved when dragging a view - this allows a "placeholder" for dragged items
* such as a background color, in the future a dotted/broken line outline, etc.
*/
public class CellBackgroundColorItemDecorator extends ItemDecoration {
private final Paint colorPaint;
public CellBackgroundColorItemDecorator(@ColorInt int color) {
colorPaint = new Paint();
colorPaint.setColor(color);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, State state) {
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
float left = child.getX() - child.getTranslationX();
float right = left + child.getWidth();
float top = child.getY() - child.getTranslationY();
float bottom = top + child.getHeight();
c.drawRect(left, top, right, bottom, colorPaint);
}
super.onDraw(c, parent, state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment