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

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

    I found something interesting about this.

    I tried setSelection and scrolltoXY but it did not work at all, the list remained in the same position, after some trial and error I got the following code that does work

    final ListView list = (ListView) findViewById(R.id.list);
    list.post(new Runnable() {            
        @Override
        public void run() {
            list.setSelection(0);
        }
    });
    

    If instead of posting the Runnable you try runOnUiThread it does not work either (at least on some devices)

    This is a very strange workaround for something that should be straight forward.

提交回复
热议问题