Firestore分页已到达最后一项

时间:2019-01-04 14:12:35

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

我遵循了该教程:如何在YouTube上用StackOverFlow成员 Alex Mamo (非常感谢)创建的Firestore Pagination创建RecyclerView。 我的问题是,当我滚动到最后一个项目并更改片段时,…而当我返回时,我的RecyclerView不再加载所有数据; RecyclerView加载firstQuery并到达lastItemVisible

在此先感谢

这是我使用的代码:

**Code for MainActivity:**
private void setFragment(Fragment fragment) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame_layout, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

代码MyRecyclerAdapter:

onBindViewHolder
tournamentViewHolder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d(TAG, "onClick: title: " + list.get(position).getTitle());

            FragmentHoles fragmentHoles = new FragmentHoles();
            Bundle bundle = new Bundle();
            bundle.putString("title", list.get(position).getTitle());

            fragmentHoles.setArguments(bundle);
            AppCompatActivity activity = (AppCompatActivity) v.getContext();
            activity.getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.main_frame_layout, fragmentHoles)
                    .addToBackStack(null).commit();
        }
    });

代码片段-> setRecyclerView

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                final List<Tournament> list = new ArrayList<>();
                for (DocumentSnapshot document : task.getResult()) {
                    Tournament tournamentModel = document.toObject(Tournament.class);
                    list.add(tournamentModel);
                }
                tournamentAdapter = new TournamentAdapter(list);
                recyclerView.setAdapter(tournamentAdapter);
                lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                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(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);

                        LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
                        int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                        int visibleItemCount = linearLayoutManager.getChildCount();
                        int totalItemCount = linearLayoutManager.getItemCount();

                        if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
                            isScrolling = false;
                            Query nextQuery = productsRef.orderBy("id", Query.Direction.DESCENDING).startAfter(lastVisible).limit(limit);
                            nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<QuerySnapshot> t) {
                                    if (t.isSuccessful()) {
                                        for (DocumentSnapshot d : t.getResult()) {
                                            Tournament productModel = d.toObject(Tournament.class);
                                            list.add(productModel);
                                        }
                                        tournamentAdapter.notifyDataSetChanged();
                                        lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);

                                        if (t.getResult().size() < limit) {
                                            Log.d(TAG, "onComplete: size: " + t.getResult().size());
                                            Log.d(TAG, "onComplete: reached: ");
                                            isLastItemReached = true;
                                        }
                                    }
                                }
                            });
                        }
                    }
                };
                recyclerView.addOnScrollListener(onScrollListener);
            }
        }
    });

0 个答案:

没有答案