要实现RecyclerView列表项滑动删除确认功能,可以采取以下步骤:
public interface OnItemDeleteListener {
void onItemDelete(int position);
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Item> itemList;
private OnItemDeleteListener onItemDeleteListener;
// Adapter的构造方法
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// 绑定数据到ViewHolder
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemDeleteListener != null) {
onItemDeleteListener.onItemDelete(position);
}
}
});
}
public void setOnItemDeleteListener(OnItemDeleteListener listener) {
this.onItemDeleteListener = listener;
}
}
MyAdapter adapter = new MyAdapter(itemList);
adapter.setOnItemDeleteListener(new MyAdapter.OnItemDeleteListener() {
@Override
public void onItemDelete(int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("确认删除该项?");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
itemList.remove(position);
adapter.notifyItemRemoved(position);
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
});
recyclerView.setAdapter(adapter);
通过以上步骤,就可以实现RecyclerView列表项滑动删除确认功能。当用户滑动删除某个列表项时,会弹出确认对话框,用户确认删除后再执行删除操作。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。