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
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);
}
}
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)
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);
}
}
});
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
CommentRv.scrollToPosition(adapter.itemCount.minus(1))
}
})