我编写了一个记事应用程序。在这里,您可以查看笔记,对其进行编辑并通过滑动将其删除。启动应用程序时,一切正常,直到我添加新笔记。创建新笔记并返回MainActivity后,recyclerview中的项目将不可见。然后,我必须在recyclerview上滚动,以使这些项目再次可见。从现在开始,即使删除一个项目也会导致不可见的项目。 Here是一个视频(添加新笔记后会录制该视频-删除项目后将不可见,您必须在recyclerview上滚动才能将其取回)。 奇怪的是,在我创建新笔记之前,一切正常。那么我的firestorelistener或ArrayList可能有问题吗?
主要活动代码:
public class MainActivity extends AppCompatActivity implements RecyclerItemTouchHelper.RecyclerItemTouchHelperListener {
private FirebaseFirestore db;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager recyclerViewLayoutManager;
private FirestoreRecyclerAdapter adapter;
private List<Note> notesList;
private ListenerRegistration firestoreListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rvNoteList);
db = FirebaseFirestore.getInstance();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new com.raycroud.notes.notes.utils.DividerItemDecoration(MainActivity.this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(adapter);
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, MainActivity.this);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
loadNotesList();
notesList = new ArrayList<>();
firestoreListener = db.collection("users").document(firebase_user_uid).collection("notes")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("LOL", "Listen failed!", e);
return;
}
notesList.clear();
for (DocumentSnapshot doc : documentSnapshots) {
Note note = doc.toObject(Note.class);
note.setId(doc.getId());
notesList.add(note);
}
adapter.notifyDataSetChanged();
}
});
}
private void loadNotesList() {
Query query = db.collection("users").document(firebase_user_uid).collection("notes");
FirestoreRecyclerOptions<Note> response = new FirestoreRecyclerOptions.Builder<Note>()
.setQuery(query, Note.class)
.build();
adapter = new FirestoreRecyclerAdapter<Note, NoteViewHolder>(response) {
@Override
protected void onBindViewHolder(NoteViewHolder holder, int position, Note model) {
final Note note = notesList.get(position);
holder.title.setText(note.getTitle());
holder.content.setText(note.getContent());
Glide.with(MainActivity.this).load(note.getNote_image_url()).into(holder.bg_note_image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateNote(note);
}
});
}
@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_view, parent, false);
return new NoteViewHolder(view);
}
@Override
public void onError(FirebaseFirestoreException e) {
Log.e("error", e.getMessage());
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof NoteViewHolder) {
int pos = viewHolder.getAdapterPosition();
String name = notesList.get(pos).getTitle();
String id = notesList.get(pos).getId();
// backup of removed item for undo purpose
final Note deletedItem = notesList.get(viewHolder.getAdapterPosition());
final int deletedIndex = viewHolder.getAdapterPosition();
// remove the item from recycler view
deleteNote(id);
Snackbar snackbar = Snackbar.make(coordinatorLayout, name + " removed from cart!", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
// undo is selected, restore the deleted item
}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
}
private void deleteNote(String id) {
db.collection("users").document(firebase_user_uid).collection("notes")
.document(id)
.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mFirebaseAnalytics.logEvent(NOTE_DELETED_EVENT, null);
}
});
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}}
答案 0 :(得分:0)
因此,至少我确定了自己的答案。我重写了我的代码。因此,当我查看https://github.com/firebase/FirebaseUI-Android/blob/master/firestore/README.md#using-the-firestorerecycleradapter时,我注意到不需要创建自己的ArrayList。删除该列表并像在Firebase示例中那样编写代码后,它就起作用了。 但是谢谢您的帮助。