5

I want to set new items to top of recyclerview. But when I set items, recyclerview scroll to top automatically.
I tried many codes but I can't resolve that.
here is my addItem method:

 public void addItem(final ArrayList<String> list) { for (int i=0; i< list.size();i++){ items.add(i,list.get(i)); } activity.runOnUiThread(new Runnable() { @Override public void run() { // notifyDataSetChanged(); // notifyItemRangeInserted( 0,list.size() ); // notifyItemInserted(); notifyItemRangeInserted(10,11); // notifyDataSetChanged(); } }); } 

I tried all of commented method.
could you please help me to resolve this??
Thanks

2 Answers 2

5

Finaly I got a solution. I put notifyItemRangeChanged after set new Items. my solution is:

 public void addItem(final ArrayList<String> list,int h) { final int oldsize = items.size(); for (int i=list.size()-1; i>=0 ;i--){ items.add(0,list.get(i)); } activity.runOnUiThread(new Runnable() { @Override public void run() { notifyItemRangeInserted(0,items.size()-oldsize); } }); } 

I hope this is been useful for everyone. Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

Why the activity.runOnUiThread part? Don't you do this in the UI thread already? If it's not on the UI thread, you could theoratically have multi-threads issues, because list is used in both threads
0

You can use recyclerView.scrollToPosition(pos); where pos is position where you want to focus your listview at after some operation on list.

3 Comments

my question is about recyclerView. not about listView
similar mehtod is there for recyclerview as well, wait I will edit the answer
you can use scrollToPosition() method for recycler view which works like setSelection() method of listview

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.