Maintain/Save/Restore scroll position when returning to a ListView

后端 未结 20 1709
無奈伤痛
無奈伤痛 2020-11-21 21:30

I have a long ListView that the user can scroll around before returning to the previous screen. When the user opens this ListView again, I want the

20条回答
  •  一个人的身影
    2020-11-21 22:08

    BEST SOLUTION IS:

    // save index and top position
    int index = mList.getFirstVisiblePosition();
    View v = mList.getChildAt(0);
    int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());
    
    // ...
    
    // restore index and position
    mList.post(new Runnable() {
        @Override
        public void run() {
          mList.setSelectionFromTop(index, top);
       }
    });
    

    YOU MUST CALL IN POST AND IN THREAD!

提交回复
热议问题