Firebase数据库分页滚动操作

时间:2018-08-18 11:23:12

标签: java android database firebase pagination

我正在关注本教程: https://codelabs.developers.google.com/codelabs/firebase-android/#6

我正在尝试实现分页,这是我修改的内容:

...
Query query = databaseRef.limitToLast(50);
...
FirebaseRecyclerOptions<FriendlyMessage> options =
            new FirebaseRecyclerOptions.Builder<FriendlyMessage>()
                    .setQuery(query, parser)
                    .build();
...

以下是默认的滚动代码:

mFirebaseAdapter.registerAdapterDataObserver(new 
RecyclerView.AdapterDataObserver() {
   @Override
   public void onItemRangeInserted(int positionStart, int itemCount) {
       super.onItemRangeInserted(positionStart, itemCount);
       int friendlyMessageCount = mFirebaseAdapter.getItemCount();
       int lastVisiblePosition =
              mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
       // If the recycler view is initially being loaded or the 
       // user is at the bottom of the list, scroll to the bottom 
       // of the list to show the newly added message.
       if (lastVisiblePosition == -1 ||
               (positionStart >= (friendlyMessageCount - 1) &&
                       lastVisiblePosition == (positionStart - 1))) {
           mMessageRecyclerView.scrollToPosition(positionStart);
       }
   }
});

mMessageRecyclerView.setAdapter(mFirebaseAdapter);

现在屏幕上仅显示50条消息。

但是在收到新消息时它不会滚动到底部。在使用查询之前可以正常工作。

我想知道我应该从哪里开始修改。

谢谢。

1 个答案:

答案 0 :(得分:0)

从敏捷的方面:

通过更改startKey,我们可以从所需位置(从数据库末尾)查询数据,并通过滚动到屏幕顶部来实现分页。

if (startKey == nil){
        print("firebasetest_startkey: ",self.startKey)
        // for first grabbing data operation

        _refHandle = self.ref.child(channel_title).queryOrderedByKey().queryLimited(toLast: 30).observe(.value){(snapshot) in
            guard let children = snapshot.children.allObjects.first as? DataSnapshot else{return}
            if (snapshot.childrenCount > 0){
                for child in snapshot.children.allObjects as! [DataSnapshot]{
                    if(!(self.messageKeys.contains((child as AnyObject).key))){
                        self.messages.append(child)
                        self.messageKeys.append(child.key)
                        self.itemTable.insertRows(at: [IndexPath(row: self.messages.count-1, section: 0)], with: .automatic)
                    }
                }
                self.startKey = children.key
            }
        }
    }else if (dragdirection == 0 && startKey != nil){
        //going up
        // for all other grabbing data operation from the bottom of the database

        _refHandle = self.ref.child(channel_title).queryOrderedByKey().queryEnding(atValue: self.startKey).queryLimited(toLast: 10).observe(.value){(snapshot) in
            guard let children = snapshot.children.allObjects.first as? DataSnapshot else{return}
            if (snapshot.childrenCount > 0 ){
                for child in snapshot.children.reversed(){
                    if ((child as AnyObject).key != self.startKey &&
                        !(self.messageKeys.contains((child as AnyObject).key))){
                        self.messages.insert(child as! DataSnapshot, at:0)
                        self.messageKeys.append((child as AnyObject).key)
                        self.itemTable.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
                    }
                }
                self.startKey = children.key
            }
        }
    }