添加数据后如何使Android Firestore分页正常工作?

时间:2018-11-30 17:11:25

标签: java android firebase pagination google-cloud-firestore

我正在尝试将Firestore分页添加到我的Android应用程序中,以避免在绑定RecyclerView中的大量数据时花费很长时间,但事实是,当我添加数据时,我设法使其只能在OnCreate方法中工作之后或刷新RecyclerView后,它将不再起作用。 我想在我的recyclerView 10中按10显示项目,以便它仅加载10个第一项目,然后当用户到达10个第一项目时,再加载10个项目,等等...

这是我遵循的教程:https://www.youtube.com/watch?v=KdgKvLll07s

这是我从firebase检索数据的方法:

private void loadPostsFromFirebase() {
    if(postArrayList.size()>0)
        postArrayList.clear();



    Query firstQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.DESCENDING).limit(10);

    firstQuery
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    for (DocumentSnapshot querySnapshot: task.getResult()){

                        Post post= new Post(querySnapshot.getString("pseudo"), querySnapshot.getString("age"), querySnapshot.getDate("timestamp"),
                                querySnapshot.getString("category"), querySnapshot.getString("gender"));

                        postArrayList.add(post);

                    }
                    adapter = new RecyclerViewAdapter(MainFeed.this, postArrayList);
                    mRecyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                    lastVisible = task.getResult().getDocuments().get(task.getResult().size() -1);
                    Toast.makeText(getApplicationContext(), "First page loaded", Toast.LENGTH_SHORT).show();

                    //ON SCROLL
                    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                            super.onScrollStateChanged(recyclerView, newState);

                            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
                               isScrolling = true;
                            }

                        }

                        @Override
                        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);

                            int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                            int visibleItemCount = linearLayoutManager.getChildCount();
                            int totalItemCount = linearLayoutManager.getItemCount();

                            if(isScrolling && (firstVisibleItem + visibleItemCount == totalItemCount) && !isLastItemReached){
                                isScrolling = false;

                                Query nextQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.DESCENDING).startAfter(lastVisible ).limit(10);
                                nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> task) {

                                        for (DocumentSnapshot querySnapshot: task.getResult()){

                                            Post post= new Post(querySnapshot.getString("pseudo"), querySnapshot.getString("age"), querySnapshot.getDate("timestamp"),
                                                    querySnapshot.getString("category"), querySnapshot.getString("gender"));

                                            postArrayList.add(post);

                                        }
                                        adapter.notifyDataSetChanged();

                                        lastVisible = task.getResult().getDocuments().get(task.getResult().size() -1);
                                        Toast.makeText(getApplicationContext(), "Next page loaded", Toast.LENGTH_SHORT).show();

                                        if(task.getResult().size() < 10){
                                            isLastItemReached = true;
                                        }

                                    }
                                });
                            }
                        }
                    };
                    //END OF OnScroll()
                    mRecyclerView.addOnScrollListener(onScrollListener);
                    }

            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(MainFeed.this, "Error", Toast.LENGTH_SHORT).show();
                }
            });

}

0 个答案:

没有答案