当用户滚动到RecyclerView的底部时,使用ScrollListener从Firebase Realtime DB中提取数据时出现问题

时间:2019-05-30 08:31:38

标签: java android firebase firebase-realtime-database android-recyclerview

我有一个从Firebase Realtime DB中提取对象数据的应用程序,将其添加到ArrayList,然后将其添加到RecyclerView。我在RecyclerView上使用了onScrollListener,以便当用户滚动到RecyclerView的底部时,将调用一个函数来从Firebase中提取更多数据,将其添加到ArrayList并将数据集更改通知给RecyclerViewAdapter。

当我运行应用程序时,它会在RecyclerView中显示数据的初始提取,然后立即开始调用该函数以从数据库中连续获取更多数据,直到没有剩余数据要提取并且以此更新我的RecyclerView数据;无论我是否滚动(沿任何方向/任何大小),都不会发生这种情况。

理想情况下,我希望应用程序仅在用户滚动到RecyclerView底部时才拉数据,并一次拉一次。我很难调试这个问题,因为逻辑似乎是正确的。任何帮助将不胜感激。

相关的全局变量

RecyclerView rvVertical;
RecyclerViewAdapter rvaVertical;
long mTotalChildren = 0;
int mLastKey = 0;

ChildEventListener childEventListenerMain, childEventListenerPager;
boolean Loading = false;

private ArrayList<BlogObject> blogArray = new ArrayList<>();
private ArrayList<BlogObject> tempList = new ArrayList<>();

查看已加载

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.socialmediatab, container, false);

    rvVertical = (RecyclerView) view.findViewById(R.id.bloglistRecyclerView);
    Loading = true;
    Log.d(TAG, "onCreateView - Loading = " + Loading);
    firstEventListener();
    regAdapterVertical();

    addOnScrollListener();
    return view;
}

第一个被调用的函数。初始化从Firebase提取的第一笔数据

private void firstEventListener() {

    FirebaseDatabase.getInstance().getReference().child("blogs").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            mTotalChildren = dataSnapshot.getChildrenCount();
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) { }
    });

    final Query BlogQuery = FirebaseDatabase.getInstance().getReference().child("blogs").orderByChild("createdAt").limitToLast(3);

    childEventListenerMain = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot ds, String s) {
            Loading = true;
            Log.d(TAG, "First get blogs instance - Loading = " + Loading);
            BlogObject addblog = ds.getValue(BlogObject.class);

            if (addblog != null) {
                BlogObject addBlog = new BlogObject(ds.getValue(BlogObject.class).getImage(), ds.getValue(BlogObject.class).getTitle(), ds.getValue(BlogObject.class).getTimeStamp(), ds.getValue(BlogObject.class).getCreatedAt());
                blogArray.add(addBlog);
                rvaVertical.notifyDataSetChanged();
                mLastKey = blogArray.get(0).getCreatedAt();
                rvVertical.scrollToPosition(blogArray.size() - 1);
            }
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) { }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) { }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) { }

        @Override
        public void onCancelled(DatabaseError databaseError) { }
    };

    BlogQuery.addChildEventListener(childEventListenerMain);
    ValueEventListener BlogChildSingleListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Loading = false;
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) { }
    };

    BlogQuery.addListenerForSingleValueEvent(BlogChildSingleListener);
}

将滚动侦听器添加到我的“回收者视图”。理想情况下,仅当用户滚动到RecyclerView的底部时,才应该从Firebase获取更多数据

private void addOnScrollListener() {
    rvVertical.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            Log.d(TAG, "Just Entered onScrolled and Loading = " + Loading);
            super.onScrolled(recyclerView, dx, dy);

            if(!rvVertical.canScrollVertically(1)) {
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (blogArray.size() >= 3 && !Loading && linearLayoutManager.findLastCompletelyVisibleItemPosition() == blogArray.size() - 1 && blogArray.size() < mTotalChildren) {
                    Loading = true;
                    getMoreBlogs();
                    Log.d(TAG, "Right After getMoreBlogs gets called. Loading = " + Loading);
                }
            }
        }
    });
}

首次拉取数据时初始化适配器

private void regAdapterVertical() {
    LinearSnapHelper linearSnapHelper = new SnapHelperOneByOne();
    linearSnapHelper.attachToRecyclerView(rvVertical);
    rvaVertical = new RecyclerViewAdapter(blogArray);
    rvVertical.setAdapter(rvaVertical);
    rvVertical.setLayoutManager((RecyclerView.LayoutManager) new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
}

这是我从Firebase中提取更多数据的功能。它在我的滚动监听器中被调用

public void getMoreBlogs() {
    tempList.clear();

    Query getMoreQuery = FirebaseDatabase.getInstance().getReference().child("blogs").orderByChild("createdAt").endAt(mLastKey).limitToLast(3);
    childEventListenerPager = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot ds, String s) {
            Loading = true;
            BlogObject blogObject = ds.getValue(BlogObject.class);
            if (blogObject != null) {
                BlogObject addBlog = new BlogObject(ds.getValue(BlogObject.class).getImage(), ds.getValue(BlogObject.class).getTitle(), ds.getValue(BlogObject.class).getTimeStamp(), ds.getValue(BlogObject.class).getCreatedAt());
                tempList.add(addBlog);
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) { }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) { }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) { }

        @Override
        public void onCancelled(DatabaseError databaseError) { }
    };

    getMoreQuery.addChildEventListener(childEventListenerPager);

    ValueEventListener addtoAdapter = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            tempList.remove(tempList.size() - 1);
            blogArray.addAll(0, tempList);
            mLastKey = blogArray.get(0).getCreatedAt();
            rvaVertical.notifyDataSetChanged();
            Loading = false;
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

    getMoreQuery.addListenerForSingleValueEvent(addtoAdapter);
}
}

这是我的Loading(boolean)的Logcat,我一直在跟踪整个程序,因为我认为这是问题的主要部分。我将其附在下面:

2019-05-30 10:26:14.590 24930-24970/com.yourgesture.newsocialmediatab I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
2019-05-30 10:26:14.956 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: onCreateView - Loading = true
2019-05-30 10:26:16.399 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: First get blogs instance - Loading = true
2019-05-30 10:26:16.406 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: First get blogs instance - Loading = true
2019-05-30 10:26:16.534 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.539 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:16.663 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.666 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:16.826 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.828 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:16.976 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.977 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:17.229 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false

1 个答案:

答案 0 :(得分:0)

在您的firstEventListener()中,

getReference()。child(“博客”) 实际上将获取整个子数据,即使您不使用它也将被视为读取操作。

因此,您的方法不会节省任何数据。

让我分享一种方法:

private void getUsers(String nodeId) {
    Query query;

    if (nodeId == null)
        query = FirebaseDatabase.getInstance().getReference()
                .child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
                .orderByKey()
                .limitToFirst(mPostsPerPage);
    else
        query = FirebaseDatabase.getInstance().getReference()
                .child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
                .orderByKey()
                .startAt(nodeId)
                .limitToFirst(mPostsPerPage);

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            UserModel user;
            List<UserModel> userModels = new ArrayList<>();
            for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                userModels.add(userSnapshot.getValue(UserModel.class));
            }

            mAdapter.addAll(userModels);
            mIsLoading = false;
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            mIsLoading = false;
        }
    });
}

三个新变量在上面的函数中很重要: mPostsPerPage:这是MainActivity.java类中定义的Integer常量,它指定每页将查询多少记录。

mIsLoading:它是MainActivity中定义的一个布尔值,用于跟踪Firebase数据库查询是否在进行中。

Consts.FIREBASE_DATABASE_LOCATION_USERS:这是Consts.java类中定义的常量,仅存储“用户”节点的位置

参考:https://blog.shajeelafzal.com/2017/12/13/firebase-realtime-database-pagination-guide-using-recyclerview/