您好,登錄后才能下訂單哦!
要實現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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。