android - RecyclerView ItemTouchHelper swipe remove animation -


i've got remove on swipe, draws background (much inbox app), implemented itemtouchhelper - overriding onchildraw method , drawing rectangle on provided canvas:

    itemtouchhelper mith = new itemtouchhelper(         new itemtouchhelper.simplecallback(0, itemtouchhelper.right) {              public void onswiped(recyclerview.viewholder viewholder, int direction) {                 remove(viewholder.getadapterposition());             }              public boolean onmove(recyclerview recyclerview, recyclerview.viewholder v, recyclerview.viewholder target) {                 return false;             }              @override             public void onchilddraw(canvas c, recyclerview recyclerview, recyclerview.viewholder viewholder, float dx, float dy, int actionstate, boolean iscurrentlyactive) {                  view itemview = viewholder.itemview;                  drawable d = contextcompat.getdrawable(context, r.drawable.bg_swipe_item_right);                 d.setbounds(itemview.getleft(), itemview.gettop(), (int) dx, itemview.getbottom());                 d.draw(c);                  super.onchilddraw(c, recyclerview, viewholder, dx, dy, actionstate, iscurrentlyactive);             }         }); 

the remove method called above in adapter:

    public void remove(int position) {        items.remove(position);        notifyitemremoved(position);     } 

the background draws out nicely, when notifyitemremoved called (according mr. debugger), recyclerview first deletes pretty green background, , pushes 2 adjacent items together.

enter image description here enter image description here

i keep background there while (just inbox app). there way that?

i had same issue , didn't wanna introduce new lib fix it. recyclerview not deleting pretty green background, it's redrawing itself, , itemtouchhelper not drawing anymore. it's drawing dx 0 , drawing itemview.getleft() (which 0) dx (which 0) see nothing. , it's drawing much, i'll come later.

anyway background while rows animate: couldn't within itemtouchhelper , onchilddraw. in end had add item decorator it. goes along these lines:

public void ondraw(canvas c, recyclerview parent, recyclerview.state state) {     if (parent.getitemanimator().isrunning()) {         // find first child translationy > 0         // draw it's top translationy whatever want          int top = 0;         int bottom = 0;          int childcount = parent.getlayoutmanager().getchildcount();         (int = 0; < childcount; i++) {             view child = parent.getlayoutmanager().getchildat(i);             if (child.gettranslationy() != 0) {                 top = child.gettop();                 bottom = top + (int) child.gettranslationy();                                     break;             }         }          // draw whatever want          super.ondraw(c, parent, state);     } } 

this code takes account rows animating up, should consider rows coming down. happens if swipe delete last row, rows above gonna animate down space.

when said itemtouchhelper drawing meant was: looks itemtouchhelper keeps viewholders of removed rows in case need restored. it's calling onchilddraw vhs in addition vh being swiped. not sure memory management implications of behavior needed additional check in start of onchilddraw avoid drawing "fantom" rows.

if (viewholder.getadapterposition() == -1) {     return; } 

in case it's drawing left=0 right=0 don't see overhead there. if start seeing swiped away rows drawing backgrounds reason.

edit: had go @ this, see blog post , github repo.


Comments