1

在此聊天應用程序中,我使用的是FirebaseIndexRecyclerAdapter,我想加載20個20個消息項目。在開始時,我設置keyReg使用FirebaseIndexRecyclerAdapter加載20個項目

keyRef=mFirebaseDatabaseReference 
     .child("messagesKeys") 
     .child(usernameFromEmail(mFirebaseUser.getEmail())) 
     .child(room_type_1) 
     .limitToFirst(20); 

然後我得到lastMessageId

keyRef.addListenerForSingleValueEvent(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 

      for (DataSnapshot child : dataSnapshot.getChildren()) { 

        oldestPostId = child.getKey(); 
       } 
      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

,我開始FirebaseIndexRecyclerAdapter

isScrollCompleted()方法
mFirebaseAdapter = new FirebaseIndexRecyclerAdapter<FriendlyMessage,MessageViewHolder>(
      FriendlyMessage.class, 
      R.layout.item_message, 
      MessageViewHolder.class, 
      keyRef, 
      mFirebaseDatabaseReference.child("messages").child(room)) {...} 

(在recyclerview.addOnScrollListener),我m這樣做:

private void isScrollCompleted() { 
    if (totalItem - currentFirstVisibleItem == currentVisibleItemCount 
     && this.currentScrollState == SCROLL_STATE_IDLE) { 
      keyRef=mFirebaseDatabaseReference 
        .child("messagesKeys") 
        .child(usernameFromEmail(mFirebaseUser.getEmail())) 
        .child(room_type_1) 
        .orderByKey() 
        .startAt(oldestPostId) 
        .limitToFirst(limit); 



      mFirebaseAdapter.notifyDataSetChanged(); 


      keyRef.addListenerForSingleValueEvent(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        boolean is=true; 
        for (DataSnapshot child : dataSnapshot.getChildren()) { 

         oldestPostId = child.getKey(); 

        } 

       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 
    } 

所以我希望每次滾動完成後,新建一個keyRef,刷新firebaseIndexAdapter,並顯示接下來的20條消息,但這不起作用! 當我使用新的keyRef並使用mFirebaseAdapter.notifyDataSetChanged()進行刷新後,沒有新帖子和前20個帖子消失。

我的問題是它是由每一個從.startAt(oldestPostId),或負載20,則40開始,然後60消息時,都20個項目中負載20更好? 因爲如果我從oldestPostId每次開始,如何回來並再次顯示過去20條消息?

回答

0

很顯然你不新加載的項追加到RecyclerView.Adapter的項目以適當的方式...

String lastOneKey = null; int limit = 20; 

keyRef.orderByKey().limitToFirst(limit).once('value', function(snapshot) { 
    snapshot.forEach(function(childSnapshot) { 

     /* that key needs to be updated in here */ 
     lastOneKey = childSnapshot.getKey(); 
    }); 
}); 

然後在isScrollCompleted()方法:

keyRef.orderByKey().startAt(lastKnownKey).limitToFirst(limit).once('value', function(snapshot) { 
    snapshot.forEach(function(childSnapshot) { 

     /* and it needs to be updated in here */ 
     lastOneKey = childSnapshot.getKey(); 
    }); 
}); 

,而不是空白該適配器必須添加一個項目,然後通知RecyclerView.Adapter有關正在插入的項目(適配器位置,添加新項目的位置始終是已有項目的長度,因此該索引基於0) ......可以也一次插入20的範圍 - 但更好的開始簡單。甚至會建議爲RecyclerView.Adapter添加最後一個鍵的getter/setter。

@ see DataSnapshot

+0

感謝的人,但我不是如此好在firebase,我現在不怎麼如何使用功能...但我的問題不是如何得到lastOneKey,它在我的代碼中正確工作,開始我得到正確的lastOnekey,每次當我滾動,我得到正確的lastOnekey下20項..我的問題是,當滾動完成,我做新的keyRef與.startAt(lastOnekey),刷新適配器,沒有任何反應......如果我嘗試,而不是刷新,重新激活適配器,然後出現下20個項目,但以前的20個項目消失...不附加新的消息,但用新的替換...我想在下面顯示一條新消息... – bojan

+0

是否甚至可以使用firebaseindexrecycleradapter? – bojan

相關問題