Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset ViewHolder after swipe using ItemTouchHelper

I am trying to implement Swipe to Delete with RecyclerView using the ItemTouchHelper.Callback. The code for my item inside the RecyclerView looks like this

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/undo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#FF4536"
    android:gravity="center_vertical"
    android:padding="16dp"
    android:text="Delete"
    android:textColor="#FFF" />

<TextView
    android:id="@+id/area"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#FFF"
    android:gravity="center_vertical"
    android:padding="16dp"
    android:text="Data"
    android:textColor="#444" />

The code for my ItemTouchHelper.Callback implementation looks like this

public class TouchHelper extends ItemTouchHelper.Callback {

private DataAdapter mListener;

public TouchHelper(DataAdapter listener) {
    mListener = listener;
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.START | ItemTouchHelper.END);
}

@Override
public boolean isLongPressDragEnabled() {
    return false;
}

@Override
public boolean isItemViewSwipeEnabled() {
    return true;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return false;
}

@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) {
        View text = viewHolder.itemView.findViewById(R.id.area);
        text.setTranslationX(dX);
    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    mListener.remove(viewHolder.getAdapterPosition());
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    super.clearView(recyclerView, viewHolder);
    View text = viewHolder.itemView.findViewById(R.id.area);
    text.setTranslationX(0.0F);
}
}

When I delete an item, I translate it to the its width but I am unable to reset the translation. I also implemented the clearView method which is supposed to reset the item's state. Any suggestions are highly appreciated.

enter image description here

like image 349
PirateApp Avatar asked Nov 09 '15 05:11

PirateApp


2 Answers

try this

@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 && isCurrentlyActive) {
        View text = viewHolder.itemView.findViewById(R.id.area);
        text.setTranslationX(dX);
    }
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
like image 92
jk2K Avatar answered Oct 24 '22 08:10

jk2K


I'm not sure I completely understand the issue, but I think I also had it. See last gif on this blog post.

blog post

If that is your problem, turns out that was a bug in recyclerview-v7 support lib version 3.1.0. You should upgrade to 3.1.1.

like image 26
Nemanja Kovacevic Avatar answered Oct 24 '22 07:10

Nemanja Kovacevic