У меня ошибка с RecyclerView. Когда я go перехожу к одному из полученных видов, а затем нажимаю кнопку «Назад», мой RecyclerView дублируется. То есть полученный просмотр повторно отображается
public class MainActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {
private List<AutoPost> postList;
private PostListAdapter postListAdapter;
private RecyclerView listPost;
private DocumentSnapshot lastVisible;
private Boolean firstLoad = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listPost = findViewById(R.id.list_post);
listPost.setHasFixedSize(true);
listPost.setLayoutManager(new LinearLayoutManager(this));
postList = new ArrayList<>();
postListAdapter = new PostListAdapter(getApplicationContext(), postList);
listPost.setAdapter(postListAdapter);
postListAdapter.notifyDataSetChanged();
}
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null) {
sendUserToLogin();
return;
}
listPost.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Boolean countBottom = !recyclerView.canScrollVertically(1);
if(countBottom){
loadMorePosts();
}
}
});
Query firstQuery = firebaseFirestore.collection("posts")
.orderBy("time", Query.Direction.DESCENDING)
.limit(4);
firstQuery.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(firstLoad) {
lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() -1);
}
for(DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
if(doc.getType() == DocumentChange.Type.ADDED){
String docID = doc.getDocument().getId();
AutoPost posts = doc.getDocument().toObject(AutoPost.class).withId(docID);
if(firstLoad) {
postList.add(posts);
} else {
postList.add(0, posts);
}
postListAdapter.notifyDataSetChanged();
}
}
firstLoad = false;
}
});
}
public void onResume() {
super.onResume();
listPost.setAdapter(postListAdapter);
}
public void loadMorePosts() {
Query nextQuery = firebaseFirestore.collection("posts")
.orderBy("time", Query.Direction.DESCENDING)
.startAfter(lastVisible)
.limit(4);
nextQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if(!queryDocumentSnapshots.isEmpty()) {
lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() -1);
for(DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
if(doc.getType() == DocumentChange.Type.ADDED){
String docID = doc.getDocument().getId();
AutoPost posts = doc.getDocument().toObject(AutoPost.class).withId(docID);
postList.add(posts);
postListAdapter.notifyDataSetChanged();
}
}
}
}
});
}
}
Я не добавил код из адаптера только потому, что не уверен, что в нем ошибка. И я также использую два RecyclerViews в одном действии, это не считается ошибкой?
Где ошибка?