如何在Android中保存和恢复ListView位置
我有 tabView,在每个选项卡中显示列表。当我在一个选项卡中滚动,切换到另一个选项卡并返回到上一个选项卡时,位置将返回到顶部,而不是显示上一个滚动位置。我该如何实现这一目标?需要知道如何使用 onSaveInstanceState & onRestoreInstanceState 保存位置并使用保存的位置来显示上一个滚动的位置。
提前致谢。
谢谢大家的回复。我尝试了所有解决方案,但遇到了其他问题。 基本上我面临的问题如下。
当我启动我的应用程序时,我有一个列表视图作为我的第一个活动。 当我单击列表项时,它会启动包含 3 个选项卡的选项卡活动。 所有 3 个选项卡都使用名为 ListActivity 的相同活动。但 3 个选项卡包含不同的数据。 我的问题是当我在选项卡之间切换时如何保留列表的位置。通过提供上述解决方案,当我更改一个选项卡中的位置时,它也会影响其余选项卡。例如,如果我位于第一个选项卡中的位置 6,则将为第二个和第三个选项卡设置该位置,并且我对所有 3 个选项卡使用相同的 ListActivity。我不被允许分享代码。所以不得不把问题打这么长。创建的选项卡数量也是动态的。可能是 3、4 或 5。但所有选项卡都使用 1 个 ListActivity。
谁能给我举个例子如何实现这一目标。 1. 在多个选项卡中使用单个ListActivity。 2. 保留选项卡中的光标位置而不影响其他选项卡。
感谢您提供的解决方案。 提前致谢。
I have tabView, displaying list in each tab. When I scroll in one tab, switch to another tab and return to previous tab, position is returned to the top instead of displaying previous scrolled position. How do I achieve this ? Need to know how do I use onSaveInstanceState & onRestoreInstanceState to save position and use the saved position in displaying the previous scrolled position.
Thanks in Advance.
Thanks all for your reply. I tried with all the solutions but ran into other issues.
Basically the problem i am facing is as follows.
I have a listview as my first activity when I launch my app.
When I click on the list item, it launches the tab activity containing 3 tabs.
All 3 tabs uses the same activity called ListActivity. But 3 tabs contains different data.
My question is how to retain the position of the list when I switch between the tabs. With the above solutions provided, when I change the position in one tab, it affects the remaining tabs as well. For example, if I am at position 6 in first tab, this position will be set for second and third tab as well as I am using the same ListActivity for all 3 tabs. I am not allowed to share the code. So have to type the problem this long. Also number of tabs created are dynamic. It might be 3 or 4 or 5. But all tabs use 1 ListActivity.
Can anyone give me a example how to achieve this.
1. Single ListActivity used in multiple tabs.
2. Retaining the cursor position in tabs without after affecting other tabs.
Your solution provided is appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要在列表视图上再次调用 setAdapter()。做这样的事情。
如果您需要更新 ListView,请在适配器上调用 notificationDataSetChanged()。
Don't call setAdapter() again on the list view. Do something like this.
If you need to update your ListView call notifyDataSetChanged() on the adapter.
要获取 ListView 的当前位置,您可以调用
然后,一旦您导航回带有该 ListView 的选项卡,您可以调用
这将取决于您的代码的编写方式来判断是否需要将位置添加到 savingInstanceState,或者然而。
To get the current position of your ListView, you can call
Then once you navigate back to the tab with that ListView, you can call
It will depend on how your code is written to tell if the position will need to be added to the savedInstanceState, or however.
重写
onSaveInstanceState
以保存位置,并在onRestoreInstanceState
中设置回位置。 Bundle 有点像 HashMap,(伪代码):在
onSaveInstanceState
中:然后在
onRestoreInstanceState
中:Override
onSaveInstanceState
to save the position, and set the position back inonRestoreInstanceState
. The Bundle is sort of like a HashMap, (pseudo-code):In
onSaveInstanceState
:Then in
onRestoreInstanceState
:在
listView.setOnItemClickListener
中,使用saveLastPosition
方法。在listview.setAdapter(adapter);
之后调用getLastPosition
方法In
listView.setOnItemClickListener
, use thesaveLastPosition
method. And afterlistview.setAdapter(adapter);
call thegetLastPosition
method