android notifyItemRangeInserted disable autoscroll
Asked Answered
T

1

24

I'm using RecyclerView as the base for my data list. i've implemented custom RecyclerView.Adapter which is based on ArraList. on fetching data from the internet the code that i'm running is:

public void addItems(List<Item> items){

    final int size = data.size();
    data.addAll(items);
    notifyItemRangeInserted(size, items.size());
}

Problem is that for after running this code i'm getting an autoscroll to the bottom of the list (last element is now visible)

Is there a way to disable this? couldn't find any similar questions.

relevant information: my adapter have 2 viewHolders - for position 0 it has a view (with viewType 0) and for the rest of list it has view with viewType 1

Thanks for your help!

Roy

Tami answered 22/11, 2014 at 16:50 Comment(14)
Have you tried to call stopScroll()?Leanora
thanks for your help. i tried it now, it's not working. also i'm not crazy about post insert solution because it can start scrolling a little before stopScroll is running. but anyhow it's not working /-:Tami
check if you have somewhere scrollToPosition called since by default RecyclerView doesn't do auto scrollingBellabelladonna
No, i have no scrollToPosition. it's weird i've also checked the code for notifiyRangeItemInserted and no indication for autoscrollingTami
strange, i had to explicitly call scrollToPosition to see my inew items, anyway did you try old school style notifyDataSetChanged()?Bellabelladonna
yep. it's working. but doc say that i should use it as last resource. my list can have a lot of data an i hate to think about the penalty for redrawing on data load /-:Tami
there shouldn't be any penalty, only you will not be seeing that fancy insert animations if you have anyBellabelladonna
which layout manager are you using? maybe you've set reverseLayout=true by mistake ?Limbert
Hi yigit, i'm using LinerLayoutManagerTami
how are you setting up the LLM? can you share some code.Limbert
Did you manage to solve this issue?Thurgau
yes...it was an error on my behalf...i should remove this questionTami
Can you give me a hint? I'm experiencing the same thing.Thurgau
My Starting index was incorrect...Tami
J
40

Problem is in your positionStart. Should be:

public void addItems(List<QuestItem> items){    
    final int positionStart = data.size() + 1;
    data.addAll(items);
    notifyItemRangeInserted(positionStart, items.size());
}
Joint answered 26/5, 2015 at 10:19 Comment(7)
yeah well...as you can see i've added a comment which say so (from feb 23). thanks anyhow mate!Tami
@Joint @Tami As per the doc, positionStartis the position of the first item that was inserted. So shouldn't it be data.size() coz that is where the first item was inserted? :pPlagiary
@AbhishekV yep :D But regarding this question it should be +1Joint
@Joint Thanks for the update. I am not sure though. let me know if it is otherwise :)Plagiary
@AbhishekV 0 position is his VIEW_TYPE_0, so you have to +1Joint
Thanks mate! solved another issue for me! I was using postionStart = data.size()Mammalogy
@AbhishekV Well I guess the VIEW_TYPE_0 is a static header and isn't managed in data, hence the +1.Uriah

© 2022 - 2024 — McMap. All rights reserved.