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

后端 未结 4 1803
不知归路
不知归路 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条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 11:05

    First get the firstVisiblePosition (or the Last one since its upside down. Yo got to try and check it yourself) and then get the top of the View which is added to the List and then using setSe;ectionFromTop() method, you can scroll to the desired location.

    Code:

    int index = mList.getFirstVisiblePosition();
    View v = mList.getChildAt(0); //or upside down (list.size - 1)
    int top = (v == null) ? 0 : v.getTop(); //returns the top of the view its Y co-ordinates. [Doc][1]
    mList.setSelectionFromTop(index, top);
    

    Source.

提交回复
热议问题