Android Firebase chat RecyclerView auto scroll to bottom when new item is added

前端 未结 4 402
暗喜
暗喜 2020-12-18 01:35

im developing an android chat using firebase.

i tried to research about this problem but i cant find a good solutions.

hope someone can help me.

my p

相关标签:
4条回答
  • 2020-12-18 01:46

    Just try below code :

             @Override
                public void onStart() {
                    super.onStart();
                    mFirebaseAdapter1 = new FirebaseRecyclerAdapter<ChatModel, ChatMessageViewHolder>(ChatModel.class,
                            R.layout.textview,
                            ChatMessageViewHolder.class,
                            ref_chatchildnode1) {
                        @Override
                        protected void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatModel m, int i) {
    
    
    
                            chatMessageViewHolder.sender.setText(m.getSender());
                            chatMessageViewHolder.msg.setText(m.getMessage());
    
                        }
                    };
                    mRecyclerView.setAdapter(mFirebaseAdapter1);
    
            if (mFirebaseAdapter1 != null) { 
                        mRecyclerView.postDelayed(new Runnable() {
                         @Override
                         public void run() { 
                             mRecyclerView.smoothScrollToPosition(mFirebaseAdapter1.getItemCount() - 1);
                         }
                      }, 5); 
                  }
                }
    
    0 讨论(0)
  • 2020-12-18 01:53

    I know it is an old thread, but maybe this will be helpful.

    In place where you add new message:

    yourRVAdapter.addMessage("New message")
    val position = yourRVAdapter.itemCount.minus(1) // itemCount will return the size of RV items 
    //Now just call your recyclerView.scrollToPosition(int)
    chatRV.scrollToPosition(position)
    
    0 讨论(0)
  • 2020-12-18 01:56

    Have a look at step 6 of the Firebase codelab for Android, which uses an AdapterDataObserver for this:

    mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
       @Override
       public void onItemRangeInserted(int positionStart, int itemCount) {
           super.onItemRangeInserted(positionStart, itemCount);
           int friendlyMessageCount = mFirebaseAdapter.getItemCount();
           int lastVisiblePosition =
                  mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
           // If the recycler view is initially being loaded or the 
           // user is at the bottom of the list, scroll to the bottom 
           // of the list to show the newly added message.
           if (lastVisiblePosition == -1 ||
                   (positionStart >= (friendlyMessageCount - 1) &&
                           lastVisiblePosition == (positionStart - 1))) {
               mMessageRecyclerView.scrollToPosition(positionStart);
           }
       }
    });
    
    0 讨论(0)
  • 2020-12-18 02:04
    adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
            override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
                
             CommentRv.scrollToPosition(adapter.itemCount.minus(1))
            }
        })
    
    0 讨论(0)
提交回复
热议问题