如何在使用ItemTouchHelper滑动后重置ViewHolder

时间:2015-11-09 05:20:38

标签: android android-recyclerview

我正在尝试使用ItemTouchHelper.Callback使用RecyclerView实现Swipe to Delete。 RecyclerView中我的项目代码如下所示

<?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" />

我的ItemTouchHelper.Callback实现的代码如下所示

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);
}
}

当我删除某个项目时,我将其翻译为其宽度,但我无法重置翻译。我还实现了clearView方法,该方法应该重置项目的状态。任何建议都非常感谢。

enter image description here

2 个答案:

答案 0 :(得分:2)

试试这个

@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);
}

答案 1 :(得分:2)

我不确定我是否完全理解这个问题,但我认为我也有这个问题。请参阅此博客文章中的最新消息。

blog post

如果这是你的问题,那么这就是recyclerview-v7支持lib 3.1.0版中的一个错误。你应该升级到3.1.1。