如何保持ListView控件的位置 [英] How to maintain the position of ListView
本文介绍了如何保持ListView控件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview">Maintain/Save/Restore滚动位置时,返回到ListView
我怎样才能保持我的ListView在我的活动的立场,当我去到另一个活动(通过启动另一个目的),然后再回来(PSS后退按钮$ P $)?
How can I maintain the position of my ListView in my activity when I go to another activity (by launching another intent) and then come back (press the back button)?
感谢你。
推荐答案
声明全局变量:
int index = 0;
ListView list;
和做一个参考,以你的的ListView
在的onCreate()
:
and make a reference to your ListView
in onCreate()
:
list = (ListView) findViewById(R.id.my_list);
接下来,在 onResume()
,在末尾加上这一行:
Next, in onResume()
, add this line at the end:
list.setSelectionFromTop(index, 0);
最后,的onPause
,以下行添加到末尾:
Lastly, in onPause
, add the following line to the end:
index = list.getFirstVisiblePosition();
这篇关于如何保持ListView控件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文