I want my RecyclerView to be scrolled to the bottom when my chat loads up but when the user manually scrolls up the list to read previous messages it should not scroll back down again upon the addition of new item in the list unless the message(new item) is sent/added by the user himself.
I have tried using both layoutManager.setStackFromEnd(true) and recyclerView.scrollToPosition(list.size() - 1). Both of these methods automatically scroll down the list upon receiving a new message if I have scrolled up manually it should not scroll down to the bottom again unless im the one sending the message.
mref.child(RoomName).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()) {
ChatMessage value = dataSnapshot1.getValue(ChatMessage.class);
ChatMessage fire = new ChatMessage();
String msgtxt = value.getMessageText();
String user=value.getMessageUser();
long msgtime=value.getMessageTime();
String prothumb=value.getProfuri();
String sentimguri=value.getSentimguri();
String type=value.getType();
fire.setMessageUser(user);
fire.setMessageText(msgtxt);
fire.setMessageTime(msgtime);
fire.setProfuri(prothumb);
fire.setSentimguri(sentimguri);
fire.setType(type);
list.add(fire);
}
adapter = new RecyclerViewAdapter(ChatRoom.this, list);
recyclerView.setAdapter(adapter);
//layoutManager.setStackFromEnd(true);
}
//Initializing RecyclerView
private void initRecyclerView() {
//Log.d(TAG, "initRecyclerView: init recyclerview.");
recyclerView =(RecyclerView) findViewById(R.id.list_of_messages);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//recyclerView.scrollToPosition(list.size() - 1);
}
}