Android-滑动删除回收视图

我正在尝试实现滑动删除相同的 Gmail 应用程序“滑动归档”:

gmail swipe delete gmail swipe delete

我试过很多教程,但没有一个能像 gmail 那样快, 我不喜欢在外部图书馆工作。我怎么做呢?

编辑:

到目前为止我的代码

  ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
public boolean onMove(RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//                    final int fromPos = viewHolder.getAdapterPosition();
//                    final int toPos = viewHolder.getAdapterPosition();
//                    // move item in `fromPos` to `toPos` in adapter.
return true;// true if moved, false otherwise
}


@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
//Remove swiped item from list and notify the RecyclerView
mAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(mRecyclerView);

我已经设法使滑动手势,但 我不知道如何添加一个背景和图像下的取景器。我试图在 item _ XXX.xml 文件中放入另一个 FrameLayout,但是在滑动时它会将整个项目与背景一起抛出。

85418 次浏览

前几天我不得不这样做,我有一些问题,所以我决定写一篇关于它的博客文章。不需要第三方自由。

基本上,您不会通过 onChildDraw绘制“撤消状态”,它将通过 ViewHolder完成。在 onSwipe中,您也不会真正删除行,只是将其标记为“挂起删除”,并通知适配器将其重新绑定到“撤消状态”。与此同时,发布一个 Runnable实际上在 x 秒内删除行,除非按下撤消按钮..。

回收视图滑动的简单代码:

     ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN | ItemTouchHelper.UP) {


@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Toast.makeText(ListActivity.this, "on Move", Toast.LENGTH_SHORT).show();
return false;
}


@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
Toast.makeText(ListActivity.this, "on Swiped ", Toast.LENGTH_SHORT).show();
//Remove swiped item from list and notify the RecyclerView
int position = viewHolder.getAdapterPosition();
arrayList.remove(position);
adapter.notifyDataSetChanged();


}
};

然后使用以下语句设置回调循环视图:

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(rv);

你可以试试 可滑动-回收视图

    SwipeableRecyclerView rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(mAdapter);


rv.setListener(new SwipeLeftRightCallback.Listener() {
@Override
public void onSwipedLeft(int position) {
mList.remove(position);
mAdapter.notifyDataSetChanged();
}


@Override
public void onSwipedRight(int position) {
mList.remove(position);
mAdapter.notifyDataSetChanged();
}
});

对于双方滑动的 xml

<com.tsuryo.swipeablerv.SwipeableRecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:leftBgColor="@color/colorAccent"
app:leftImage="@drawable/ic_remove"
app:leftText="Delete"
app:rightBgColor="@color/blue"
app:rightImage="@drawable/ic_check"
app:rightText="Read"
app:textColor="@android:color/white"
app:textSize="20sp" />

下面是 onCreate中设置“向左滑动删除”的最小代码:

binding.rows.adapter = adapter
ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
override fun onMove(v: RecyclerView, h: RecyclerView.ViewHolder, t: RecyclerView.ViewHolder) = false
override fun onSwiped(h: RecyclerView.ViewHolder, dir: Int) = adapter.removeAt(h.adapterPosition)
}).attachToRecyclerView(binding.rows)

这里假设您的 Adapter类具有 removeAt函数:

fun removeAt(index: Int) {
items.removeAt(index)   // items is a MutableList
notifyItemRemoved(index)
}

谢谢@RahulRaina 为你的 Java 答案!