如果想要实现RecyclerView的Item滑动删除功能,可以通过使用ItemTouchHelper类来实现。以下是一个简单的示例代码:
public class SwipeToDeleteCallback extends ItemTouchHelper.Callback {
private final RecyclerViewAdapter mAdapter;
public SwipeToDeleteCallback(RecyclerViewAdapter adapter) {
mAdapter = adapter;
}
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
mAdapter.deleteItem(position);
}
}
public void deleteItem(int position) {
mItems.remove(position);
notifyItemRemoved(position);
}
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(mAdapter));
itemTouchHelper.attachToRecyclerView(recyclerView);
通过以上步骤,就可以实现RecyclerView的Item滑动删除功能。当用户在Item上滑动时,Item将会被删除并且列表将会自动更新。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。