How to add a new project to recyclerview without scrolling to the top… here is a solution to the problem.
How to add a new project to recyclerview without scrolling to the top
I want to set the new project to the top of the recyclerview. But when I set up the project, the recyclerview automatically scrolls to the top.
I tried a lot of code but I couldn’t fix this.
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 the method of all comments.
Can you help me with this?
Thanks
Solution
Finally found a solution. I put notifyItemRangeChanged
after setting up the new item. 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);
}
});
}
Hope it works for everyone. Thanks