Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can RecyclerView ItemTouchHelper lock swipe item?

I use RecyclerView with ItemTouchHelper to do swiping to remove, and add delete button under the swipe item like this: Swiping spec

But when I finish swiping, the item will be removed from the list soon! How can I lock the swiped item, and removed by clicking the delete button? Just like Gmail's behavior.

ps. I post same issues here https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=189057

like image 889
Daniel Deng Avatar asked Nov 05 '15 16:11

Daniel Deng


People also ask

How do you stop swiping the item in recycler view after reaching some distance?

1 Answer. Show activity on this post. float newDx = dX; if (newDx >= 100f) { newDx = 100f } super. onChildDraw(c, recyclerView, viewHolder, newDx, dY, actionState, isCurrentlyActive);

How to swipe to delete RecyclerView?

Android Swipe To Delete. Swipe to delete feature is commonly used to delete rows from a RecyclerView. In order to implement Swipe to delete feature, we need to use the ItemTouchHelper utility class.

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.


1 Answers

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    float translationX;
    if(dX > 0){
      translationX = Math.min(-dX, viewHolder.itemView.getWidth() / 2);
    }
    else {
       translationX = Math.max(dX, (-1)* viewHolder.itemView.getWidth() / 2);
    }
    super.onChildDraw(c, recyclerView, viewHolder, translationX, dY, actionState, isCurrentlyActive);
}
like image 159
dramf Avatar answered Sep 23 '22 03:09

dramf