FirestoreRecyclerView滑动CardView删除文档

时间:2019-11-28 11:51:00

标签: java android android-recyclerview google-cloud-firestore

我在Fragment的onStart()方法中有以下代码:

final FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<NewTask, TasksViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final TasksViewHolder holder, final int position, @NonNull NewTask model) {
            holder.setTaskTitle(model.getTaskTitle());
            holder.setTaskPrivacy(model.isTaskPrivacy());
            holder.setTaskColor(model.getTaskColor());

            //<-----  ONCLICK OF A TASK  ----->
            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final DocumentReference ref = getSnapshots().getSnapshot(position).getReference();
                    Toast.makeText(getActivity(), ref.getId(), Toast.LENGTH_SHORT).show();
                    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
                        @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) {
                            Toast.makeText(getActivity(), ref.getId(), Toast.LENGTH_SHORT).show();
                        }
                    }).attachToRecyclerView(twoRecyclerView);
                }
            });
        }

使用提供的代码会发生这种情况:

当我单击CardView项目时,会弹出该项目的Firestore文档ID的Toast消息。但是,当我轻扫该项目时,会弹出先前已单击的第一个项目的Toast消息。因此,如果我尝试在onSwipe方法中删除一个项目,则会不断删除第一个选择的项目。

我的问题是:如何删除onSwipe中想要的项目?

我的主要目标是能够在向左右滑动时删除CardView项,而不必在单击该项时删除。

1 个答案:

答案 0 :(得分:0)

几个小时的翻页之后,我想出了在Fragment中设置“轻扫以从Firestore函数中删除CardView项的最佳方法”是创建一个新的Adapter Java类。

public class NoteAdapter extends FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder> {

public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull Note model) {
    holder.textViewTitle.setText(model.getTitle());
    holder.textViewDescription.setText(model.getDescription());
    holder.textViewPriority.setText(String.valueOf(model.getPriority()));
}

@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,
            parent, false);
    return new NoteHolder(v);
}

public void deleteItem(int position) {
    getSnapshots().getSnapshot(position).getReference().delete();
}

class NoteHolder extends RecyclerView.ViewHolder {
    TextView textViewTitle;
    TextView textViewDescription;
    TextView textViewPriority;

    public NoteHolder(View itemView) {
        super(itemView);
        textViewTitle = itemView.findViewById(R.id.text_view_title);
        textViewDescription = itemView.findViewById(R.id.text_view_description);
        textViewPriority = itemView.findViewById(R.id.text_view_priority);
    }
}

CodingInFlow提供的代码