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

后端 未结 20 1663
無奈伤痛
無奈伤痛 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:05

    use this below code :

    int index,top;
    
    @Override
    protected void onPause() {
        super.onPause();
        index = mList.getFirstVisiblePosition();
    
        View v = challengeList.getChildAt(0);
        top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());
    }
    

    and whenever your refresh your data use this below code :

    adapter.notifyDataSetChanged();
    mList.setSelectionFromTop(index, top);
    

提交回复
热议问题