我在RecyclerView中有一个错误。 当我转到收到的视图之一,然后按“后退”按钮时,我的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();
}
}
}
}
});
}
}
我没有从适配器添加代码,只是因为我不确定其中是否有错误。 而且我还在一个Activity中使用了两个RecyclerView,这不是错误吗?
哪里出了错?