Kalaiselvan
Kalaiselvan

Reputation: 408

How to auto scroll up Recycler view in Android?

I have developed some chat application where i can send and received messages. But the problem is whenever i send or receive messages the recycler view is not scrolling to top so that messages gets diplayed above the keypad.

I have used the above mentioned recycler view android.support.v7.widget.RecyclerView.

Upvotes: 20

Views: 49289

Answers (7)

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2718

If you want to take your user directly to the latest item without scroll animation then use this:

recyclerView.scrollToPosition(adapter.getItemCount()-1);

Upvotes: 0

pseudoankit
pseudoankit

Reputation: 329

I was using horizontal recycler view with linear layout manager ,accepted solution didn't work for me

But below solution worked,

(recycler.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(position, 0)

Upvotes: 0

nilanka jayasanka
nilanka jayasanka

Reputation: 49

try with following method for auto scroll to any position....you can change argument of the "notifyItemChanged" method.

 public void printer(String message){
    block block=new block();
    block.details=message;

    blocks.add(block);

    blockAdapter.notifyItemChanged(0);
    recyclerView.scrollToPosition(blocks.size()-1);
   // recyclerView.scrollTo(blocks.size()-1,blocks.size());
}

Upvotes: 0

Nabeel K
Nabeel K

Reputation: 6128

On updating recyclerview try to run this code :

recyclerView.post(new Runnable() {
    @Override
    public void run() {
        // Call smooth scroll  
        recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);                                 
    }
});

Upvotes: 53

CoCo Dev
CoCo Dev

Reputation: 306

I use the following code so that the scroll is automatic until the last position:

recyclerView.smoothScrollToPosition(adapter.getItemCount());

I implement to add a new item to the list.

Upvotes: 9

Surendar D
Surendar D

Reputation: 5644

Try the bellow method for auto scroll to your particular position.

adapter = new DeliveriesDateRecycleAdapter(getActivity(),sampleDatedate, position);
                recyclerviewDates.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                recyclerviewDates.smoothScrollToPosition(position);

Note : position - the position of the list that you want to show.

Upvotes: 3

Benjamin Scharbau
Benjamin Scharbau

Reputation: 2089

Have you tried RecyclerView.srollTo(int x, int y)?

Edit: Try RecyclerView.scrollTo(0, 0) directly after you have send or received the last message.

Upvotes: 1

Related Questions