0

I have a RecyclerView which contains messages item. I want to scroll to the bottom of layout when the user has pressed a button (like chat application). I have tried this piece of code but it does not work.I need this task done after a few millisecond.Because new message add to adapter in the asynchronous call retrofit.

 imgSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(edtMessage.getText().length()>0) {
                sentUserMessage();
                edtMessage.setText("");
                //I try this line or next line.None of them doesn't work
                linearLayoutManager.scrollToPositionWithOffset(messages.size(),0);
                recyclerView.scrollToPosition(messages.size());
            }
        }
    });
maryam
  • 1,437
  • 2
  • 18
  • 40

1 Answers1

1

try this

recyclerView.post(new Runnable() {
    @Override
    public void run() {
        // Call smooth scroll  
        recyclerView.smoothScrollToPosition(adapter.getItemCount().length());                                 
    }
});
Kevin Kurien
  • 812
  • 6
  • 14
  • It work. but I need this code done after a few millisecond.Because new message add to adapter in the asynchronous call retrofit. – maryam Oct 02 '18 at 07:37
  • @sarina you can add a progressDialog to show the user new data is uploading and scroll to the bottom when dialog.dismiss is called – Kevin Kurien Oct 02 '18 at 08:45
  • This solution isn't suitable for me. Because my app is chat application and it isn't logical to show a progressDialog to user. User write a message and the activity should go to bottom where new massage is placed. – maryam Oct 02 '18 at 09:14