Firebase android分页加载总是相同的项目

时间:2017-09-01 09:41:39

标签: android firebase firebase-realtime-database pagination

我正在尝试在firebase中进行分页。我已经按照这种方法(Firebase pagination limitToFirst() vs 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;

//首先加载项目

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();
                }
            }
        }
    }
});

//加载更多

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) {
                }
            });

        }

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

// POJO CLASS

      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;
    }

我的查询:

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

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

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

    Post newPost = new Post(postText, timestamp);
    postsRef.push().setValue(newPost);

JSON FIREBASE

{
  "-Kt8GeVfyiebF8XhUUkL" : {
    "text" : "0",
    "timestampCreated" : {
      "date" : 1504467921811
    }
  }
}

更新2

更改orderByChild中的标记并知道,正在进行分页。

orderByChild("timestampCreated/date") 

0 个答案:

没有答案