Firebase android 分页加载始终相同的项目

S.P*_*.P. 6 pagination android firebase firebase-realtime-database

我正在尝试在 firebase 中进行分页。我遵循了这种方法(Firebase 分页 limitToFirst() 与 limitToLast()),但我无法从firebase获得正确的分页项目,始终获得相同的 3 个项目。

这是下面代码的问题:

我的 firebase 数据库中有 12 个项目。我检索了第一个加载项目的 3 个元素,当我尝试加载更多项目时,我总是得到与第一次加载相同的元素。

我试过: postList.size()-1).getTimestamp().toString()
和 postList.size()-3).getTimestamp().toString() 但总是得到相同的数据

private ArrayList<Post> postList = new ArrayList<>();
private Query allPostsQuery;
private final static int PAGESIZE = 3;
Run Code Online (Sandbox Code Playgroud)

//启动时首先加载项目

linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
mAdapter = new WallAdapter(postList, getFragmentManager(), getActivity(), navigation, mListener, WallFragmentOwn.this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if(dy > 0) {
            visibleItemCount = linearLayoutManager.getChildCount();
            totalItemCount = linearLayoutManager.getItemCount();
            pastVisiblesItems = linearLayoutManager.findFirstVisibleItemPosition();

            if (loading) {
                if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                    loading = false;
                    loadMoreItems();
                }
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

//装载更多

public void loadMoreItems(){
            allPostsQuery = FirebaseUtil.getPostsRef().orderByChild("timestamp")
                        .endAt(postList.get(postList.size()-1).getTimestamp().toString())
                        .limitToLast(PAGESIZE);
            allPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    if(dataSnapshot.getValue() == null){
                        return;
                    }
                    dataSnapshot.getChildrenCount();
                    for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                        Post post = postSnapshot.getValue(Post.class);
                        post.setUid(postSnapshot.getKey());
                        postList.add(post);
                    }
                    mAdapter.notifyDataSetChanged();
                }

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

        }
Run Code Online (Sandbox Code Playgroud)

更新 1 我知道我需要使用与 Firebase 数据库中相同的类型来执行 .endAt() 。这是我的代码知道:

//POJO类

      private HashMap<String, Object> timestampCreated;


public Post(String text, HashMap<String, Object>  timestampCreatedObj) {
    this.text = text;
    timestampCreatedObj = new HashMap<String, Object>();
    timestampCreatedObj.put("date", ServerValue.TIMESTAMP);
    this.timestampCreated = timestampCreatedObj;

}

 @Exclude
    public long getCreatedTimestampLong(){
        return (long)timestamp;
    }
Run Code Online (Sandbox Code Playgroud)

我的查询:

allPostsQuery = getBaseRef().child("posts").orderByChild("timestamp")
            .endAt(postList.get(postList.size()-1).getTimestampCreatedLong())
            .limitToFirst(PAGESIZE);
Run Code Online (Sandbox Code Playgroud)

我以这种方式在 Firebase 中设置时间戳:

      HashMap<String, Object> timestamp = new HashMap<String, Object>();
    timestamp.put("timestamp", ServerValue.TIMESTAMP);

    Post newPost = new Post(postText, timestamp);
    postsRef.push().setValue(newPost);
Run Code Online (Sandbox Code Playgroud)

JSON 火库

{
  "-Kt8GeVfyiebF8XhUUkL" : {
    "text" : "0",
    "timestampCreated" : {
      "date" : 1504467921811
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

更新 2

更改 orderByChild 中的标签并知道,正在分页。

orderByChild("timestampCreated/date") 
Run Code Online (Sandbox Code Playgroud)