FirebaseUI:如何在FirebaseRecyclerAdapter上实现无限滚动

时间:2018-12-26 15:24:52

标签: android firebase firebase-realtime-database recycler-adapter firebaseui

我想使用FirebaseRecyclerAdapter来实现无限滚动,而我正在使用Firebase realtimedatabase(不是firestore)。

我已经检查了很多关于该主题的堆栈溢出问题,但事实是这些问题是为firestore实现的,还是通过使用Array list实现的。

这些是链接: Link1 Link2 Link3  和其他一些。

这是我的代码。

FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<User>()
            .setQuery(mQuery, User.class)

            .build();

    final FirebaseRecyclerAdapter<User, OnlineUserViewHolder> onlineUserAdapter =
            new FirebaseRecyclerAdapter<User, OnlineUserViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull final OnlineUserViewHolder holder, final int position, @NonNull User model) {

                    final String userID = getRef(position).getKey();
                    final String retrievedName;


                    mFirebaseUserDBRef.child(userID).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {

                            oldestUserId = dataSnapshot.getKey(); ////HERE WE ARE SAVING THE LAST User_ID So that we can implement scrolling
                            final String retrievedName = dataSnapshot.child("name").getValue().toString();
                            holder.name.setText(retrievedName);

                        }



                        }


                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });

                }

            };


    recyclerView.setAdapter(onlineUserAdapter);
    onlineUserAdapter.startListening();

以下是滚动代码:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrollStateChanged(@NonNull 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);

                            currentItems = linearLayoutManager.getChildCount();
                            totalItems = linearLayoutManager.getItemCount();
                            scrolledOutItems = linearLayoutManager.findFirstVisibleItemPosition();


                            if (isScrolling && (currentItems + scrolledOutItems == totalItems)){

                                isScrolling = false;

                                mQuery = mFirebaseUserDBRef.orderByChild("lastSignIn").startAt(oldestUserId).limitToLast(20);
                                mQuery.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                        //Don't know how to add it to onlineUserAdapter

                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {

                                    }
                                });


                            }

                        }
                    });

如何实现FirebaseRecycleAdapter的滚动?

谢谢

0 个答案:

没有答案