Maintain scroll position when adding to ListView with reverse endless-scrolling

后端 未结 4 1864
不知归路
不知归路 2021-02-05 10:38

I am building a chat-like Android application, similar to Hangouts. For this purpose I am using a vertical ListView with stackFromBottom=true and transcriptMo

4条回答
  •  旧时难觅i
    2021-02-05 11:00

    I have figured this out. Wherever it is that you are getting new data I call the following before changing data in the adapter:

    int firstVisibleItem = list.getFirstVisiblePosition();
    int oldCount = adapter.getCount();
    View view = list.getChildAt(0);
    int pos = (view == null ? 0 :  view.getBottom());
    

    Then I call:

    adapter.setData(data);
    
    list.setSelectionFromTop(firstVisibleItem + adapter.getCount() - oldCount + 1, pos);
    

提交回复
热议问题