0

我有一个很好的问题,试图将数据从firebase数据库分页数周,现在都无济于事。我尝试了从android文档到SO的所有示例,但仍然无法正常工作。下面是我的数据节点Firebase数据库分页升序

enter image description here

下面的截图是我的代码,我使用recycleview进行连续滚动。

commentDatabaseReference = FirebaseDatabase.getInstance().getReference().child(CHANNEL_COMMENT).child(postId); 
commentDatabaseReference.orderByKey().limitToLast(TOTAL_ITEM_EACH_LOAD).addListenerForSingleValueEvent(commentValueEventListener); 

ValueEventListener commentValueEventListener = new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      if(!dataSnapshot.hasChildren()) { 
       currentPage--; 
      } 

      int counter = 0; 
      for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
       commentId = snapshot.getKey(); 
       Comment comment = snapshot.getValue(Comment.class); 
       commentList.add(comment); 

       adapter.notifyDataSetChanged(); 
      } 
      //adapter.notifyDataSetChanged(); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }; 

Recycleview连续滚动收听

recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) { 

      @Override 
      public void onLoadMore(int page, int totalItemsCount, RecyclerView view) { 
       Log.e("more", "onload"); 
       loadMoreData(); 
      } 
     }); 

private void loadData() { 
     Log.e("loading", ""+ commentId); 
     commentDatabaseReference.orderByChild("timestamp").startAt(commentId).limitToLast(TOTAL_ITEM_EACH_LOAD) 
       .addListenerForSingleValueEvent(commentValueEventListener); 
    } 

    private void loadMoreData(){ 
     //if ((currentPage * TOTAL_ITEM_EACH_LOAD) <= commentCount) { 
      currentPage++; 
      loadData(); 
     //} 
    } 

结论:举例来说,如果我有20个数据与10偏移和我用排序依据与limitToLast我会在顶部开始的最后10个数据,即我20,而不是20 - - 11获得数据11.也注意到有的时候我无休止的数据时,我结合了startAt

回答

0

终于解决了硫S必填到FARUK提示:

以下方法负载数据的代码从火力到recycleview

private void loadData() { 
     Query query; 
     if (commentId == null) { 
      query = commentDatabaseReference.orderByChild("timestamp").limitToLast(TOTAL_ITEM_EACH_LOAD); 
     } else { 
      query = commentDatabaseReference.orderByChild("timestamp").startAt(inverseTimestamp).limitToFirst(TOTAL_ITEM_EACH_LOAD); 
     } 
     query.addListenerForSingleValueEvent(commentValueEventListener); 
    } 

    private void loadMoreData(){ 
     //commentCount: is the total comments stored in firebase database 
     if ((currentPage * TOTAL_ITEM_EACH_LOAD) <= commentCount) { 
      currentPage++; 
      loadData(); 
     } 
    } 

在recycleview滚动听者我称为loadMoreData方法从火力

recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) { 

      @Override 
      public void onLoadMore(int page, int totalItemsCount, RecyclerView view) { 
       //Load more data 
       loadMoreData(); 
      } 
     }); 

获取更多数据Firebase数据库监听器用于侦听提取的数据

ValueEventListener commentValueEventListener = new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      if(!dataSnapshot.hasChildren()) { 
       currentPage--; 
      } 

      int counter = 0; 
      for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
       commentId = snapshot.getKey();//Index key for the node 
       Comment comment = snapshot.getValue(Comment.class); 

       ++counter; 

       //Inverse timestamp 
       inverseTimestamp = comment.getTimestamp(); 

       //store nth - 1 data into the arraylist to avoid duplicates 
       if (counter < TOTAL_ITEM_EACH_LOAD) { 
        commentList.add(comment); 

        adapter.notifyDataSetChanged(); 
       } 
      } 
      //adapter.notifyDataSetChanged(); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }; 
0

使用limitToFirst并添加一个字段(时间戳逆),其中包含timestamp * -1例如叫它timestampInv

然后你就可以orderByChild("timestampInv")startAt

commentDatabaseReference 
    .orderByChild("timestampInv") 
    .startAt(lastTimeInv) 
    .limitToFirst(TOTAL_ITEM_EACH_LOAD) 
    .addListenerForSingleValueEvent(commentValueEventListener); 

如果你想自动创建时间戳逆,我建议使用cloud functiononCreate触发:

exports.addCommentTimestampInv = functions.database.ref('/comment/{postId}/{commentId}') 
.onCreate(event => { 
    // Grab the current value of what was written to the Realtime Database. 
    const commentData = event.data.val(); 

    var timeInv = commentData.timestamp * -1; 

    return event.data.ref.child('timestampInv').set(timeInv); 

}); 

希望它有助于:)

+0

谢谢@faruk我会试试我t出来,并给你反馈 – Prodigy

+0

我试过上面的,我能够从下面获取数据,但是当我到达顶部它继续拉数据不间断 – Prodigy