Joehamir Joehamir - 14 days ago 13
Android Question

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

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 problem i want the RecyclerView to auto scroll if new item is added.

here is my code.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.chatlayoutchat, container, false);

Connectivity connectivity=new Connectivity();
if(connectivity.isConnected(getActivity())) {
if(getArguments() != null) {
Firebase.setAndroidContext(this.getActivity());
String ccustomersid = getArguments().getString("customersid");
String creferralae = getArguments().getString("referralae");
String ccustomersname = getArguments().getString("customersname");

customersid = ccustomersid;
referralae = creferralae;
customersname = ccustomersname;

Firebase.setAndroidContext(this.getActivity());


editText = (EditText) rootView.findViewById(R.id.editText);

sendbutton = (Button) rootView.findViewById(R.id.button);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutmgr = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutmgr);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(linearLayoutManager);

from_user = customersid;
to_user = referralae;
LoggedInUser = customersname;


//Log.v("NODE CREATED:", from_user + " " + to_user);


ref_chatchildnode1 = firebase_chatnode.child(from_user + " " + to_user);

//ref_chatchildnode2 = firebase_chatnode.child(to_user + " " + from_user);
sendbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newmsg = editText.getText().toString().trim();
ChatModel m = new ChatModel();
m.setSender(LoggedInUser);
m.setMessage(newmsg);
ref_chatchildnode1.push().setValue(m);
//ref_chatchildnode2.push().setValue(m);
editText.setText("");


}
});
ref_chatchildnode1.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ChatModel chatmsg = dataSnapshot.getValue(ChatModel.class);
chatmsgsList.add(chatmsg);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

}

}else{
nointernet();
}

showGlobalContextActionBar();

return rootView;
}

@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);

}

Answer

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