Add a new item to the top of the RecyclerView

I add an element for recyclerview position 0 programamticly

public void addQuestion(Question question){ this.questionList.add(0, question); notifyItemInserted(0); } 

This works very well and the items are listed up, but the user must scroll up to see the new item.

Is there a trick, how does an element appear on top, and does recyclerview automatically scroll?

+6
source share
3 answers

Ok you can use mRecyclerView.smoothScrollToPosition(int position)

Example:

 public void addQuestion(Question question){ this.questionList.add(0, question); notifyItemInserted(0); mRecyclerView.smoothScrollToPosition(0); } 

UPDATE:

if you want the scroll to a specific element to be really smooth, you can take a look at the answer to this question

RecyclerView - How to smooth scrolling to the top of a position at a specific position?

+10
source

Yes you can do it

 mRecyclerView.smoothScrollToPosition(0); 
+1
source

try it

 mRecyclerView.smoothScrollToPosition(0); 
+1
source

All Articles