Firbase聊天分页如何做?

时间:2019-02-20 12:40:18

标签: firebase pagination chat

从firebase数据库获取列表时我遇到了问题,但没有得到解决方法。

要求是-通过分页从Db获取Firebase聊天列表

1 个答案:

答案 0 :(得分:0)

在这里,我完成了Firebase-Chat-Pagination。对此进行检查。它可能会对您有所帮助。

private void loadDataFirst(){     mUserRefValueEventListener = mConversationRef.child(mLocation)             .limitToLast(TOTAL_ITEM_EACH_LOAD)             .orderByKey()             .addValueEventListener(new ValueEventListener(){                 @Override                 公共无效onDataChange(DataSnapshot dataSnapshot){                     //当自己发送新消息时,它应该为null                     mLastDataSnapshot = null;                     如果(dataSnapshot.exists()){                         mChatList.clear();                         对于(DataSnapshot readDataSnapshot:dataSnapshot.getChildren()){                             ChatResponse chatModel = readDataSnapshot.getValue(ChatResponse.class);                             如果(chatModel!= null){                                 mChatList.add(chatModel);                             }                             //我们需要First DataSnapshot进行分页                             如果(mLastDataSnapshot == null){                                 mLastDataSnapshot = readDataSnapshot;                             }

                    }
                    mView.onFetchMessagesSuccess(mChatList);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

}

public void loadMoreDataPagination(){

if (mLastDataSnapshot != null && (mTotalCount == 0 || mView.getChatMessageCount() < mTotalCount)) {
    mUserRefValueEventListener = mConversationRef.child(mLocation)
            .orderByKey()
            .endAt(mLastDataSnapshot.getKey())
            .limitToLast(TOTAL_ITEM_EACH_LOAD)
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {


                    if (dataSnapshot.exists()) {
                        mChatList.clear();
                        for (DataSnapshot readDataSnapshot : dataSnapshot.getChildren()) {
                            ChatResponse chatModel = readDataSnapshot.getValue(ChatResponse.class);
                            if (chatModel != null) {
                                mChatList.add(chatModel);
                            }
                            // We need Last DataSnapshot for pagination while load more
                            if (mChatList.size() == 1) {
                                mLastDataSnapshot = readDataSnapshot;
                            }
                        }
                        // One item duplicates from last list so need to Remove it
                        mChatList.remove(mChatList.size() - 1);
                        mView.onFetchLoadMoreSuccess(mChatList, mTotalCount);

                    }
                }

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

}