21

I am adding an item to recyclerview position 0 programamticly

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

This is working very well and the items do appear in the list at top BUT the user has to scroll up to see the new item.

Is there any trick how the item appear at top and recyclerview is scrolling up automaticly ?

Atiq
  • 14,435
  • 6
  • 54
  • 69
Chris Blank
  • 213
  • 1
  • 2
  • 4

4 Answers4

21

well 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 to make the scrolling to certain item really smooth you can have a look at answer to this question

RecyclerView - How to smooth scroll to top of item on a certain position?

Community
  • 1
  • 1
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • it is not as smooth as notifyItemInserted but it is the best way to handle that ! thx – Chris Blank Aug 09 '16 at 12:44
  • 3
    `smoothScrollToPosition(0)` is lagging if there are more items than the list could show at the moment (that's what I experienced). I simply used `scrollToPosition(0)` instead, it makes no difference in "smoothness" – mbo Apr 11 '17 at 12:03
3

Yes, you can do this

mRecyclerView.smoothScrollToPosition(0);
3

Try this

mRecyclerView.smoothScrollToPosition(0);
Akshay Panchal
  • 695
  • 5
  • 15
  • exact copy of Jemo's answer :D – Willi Mentzel Aug 09 '16 at 12:24
  • 2
    Seems we both posted same time – Akshay Panchal Aug 09 '16 at 12:25
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Jonathan Lam Aug 09 '16 at 14:12
3

If i understand right and you problem is that you already scrolled to top of list, but when inserting you had to scroll again to see item, you can try my approach to avoid it.

From my experience, approach with scrolling after insertion works, but animations doesn't look natural.

If you really want to save animations you can try an approach which helped me in my project: use multi-typed recycler. Display additional item of second type at 0 position in your adapter. This item can be just a view with little padding, header (if you need) or even an empty view. Then, notifyItemInserted(1) and you will get nice insert animation.

NOTE: this approach may add complexity to your project and requires knowledge about multi-type recycler view.

j2esu
  • 1,647
  • 1
  • 16
  • 26
  • Adding an additional item of the second type at 0 position with 1dp height in the adapter helped me to solve IllegalArgumentException: called attach on a child which is not detached: viewholder. – Alexey Apr 21 '19 at 07:09