Adding a colored background with text/icon under swiped row when using Android's RecyclerView -
edit: real problem linearlayout
wrapped in layout, caused incorrect behavior. accepted answer sanvywell has better, more complete example of how draw color under swiped view code snippet provided in question.
now recyclerview widget has native support row swiping of itemtouchhelper class, i'm attempting use in app rows behave google's inbox app. is, swiping left side performs 1 action , swiping right another.
implementing actions easy using itemtouchhelper.simplecallback's onswiped
method. however, unable find simple way set color , icon should appear under view that's being swiped (like in google's inbox app).
to that, i'm trying override itemtouchhelper.simplecallback's onchilddraw
method this:
@override public void onchilddraw(canvas c, recyclerview recyclerview, recyclerview.viewholder viewholder, float dx, float dy, int actionstate, boolean iscurrentlyactive) { recyclerviewadapter.viewholder vh = (recyclerviewadapter.viewholder) viewholder; linearlayout ll = vh.linearlayout; paint p = new paint(); if(dx > 0) { p.setargb(255, 255, 0, 0); } else { p.setargb(255, 0, 255, 0); } c.drawrect(ll.getleft(), ll.gettop(), ll.getright(), ll.getbottom(), p); super.onchilddraw(c, recyclerview, viewholder, dx, dy, actionstate, iscurrentlyactive); }
determining swipe direction dx , setting appropriate color works intended, coordinates viewholder
correspond place first linearlayout
inflated.
how correct coordinates linearlayout
that's in swiped row? there easier way (that doesn't require override onchilddraw
) set background color , icon?
i struggling implement feature well, steered me in right direction.
@override public void onchilddraw(canvas c, recyclerview recyclerview, recyclerview.viewholder viewholder, float dx, float dy, int actionstate, boolean iscurrentlyactive) { if (actionstate == itemtouchhelper.action_state_swipe) { // recyclerview item viewholder view itemview = viewholder.itemview; paint p = new paint(); if (dx > 0) { /* set color positive displacement */ // draw rect varying right side, equal displacement dx c.drawrect((float) itemview.getleft(), (float) itemview.gettop(), dx, (float) itemview.getbottom(), p); } else { /* set color negative displacement */ // draw rect varying left side, equal item's right side plus negative displacement dx c.drawrect((float) itemview.getright() + dx, (float) itemview.gettop(), (float) itemview.getright(), (float) itemview.getbottom(), p); } super.onchilddraw(c, recyclerview, viewholder, dx, dy, actionstate, iscurrentlyactive); } }
Comments
Post a Comment