如何保存和恢复在Android中的ListView位置 [英] How to save and restore ListView position in Android

查看:127
本文介绍了如何保存和恢复在Android中的ListView位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有TabView的,在每个选项卡显示列表。当我滚动在一个选项卡,切换到另一个标签,并返回到previous标签中,位置返回到顶部,而不是显示previous滚动位置。我如何实现这一目标?要知道如何使用的onSaveInstanceState和放大器; onRestoreInstanceState保存位置,并使用所保存的位置显示previous滚动位置。

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 all for your reply. I tried with all the solutions but ran into other issues. Basically the problem i am facing is as follows.

我有一个列表视图作为我的第一个活动,当我启动我的应用程序。 当我点击列表中的项目,它推出包含3个标签的标签活动。 所有3个标签使用名为ListActivity相同的活动。但3个标签中包含不同的数据。 我的问题是如何留住列表中的位置,当我在标签之间切换。随着提供的,当我改变的一个选项卡的位置上面的解决方案,它会影响剩下的选项卡以及。举例来说,如果我在第一个选项卡6位,这个位置将是第二个和第三个选项卡设置,以及我使用的是相同的ListActivity所有3个标签。我不能共享code。因此,有这么长的时间键入该问题。同时创建标签的数量是动态的。这可能是3或4或5。但所有选项卡使用1 ListActivity。

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.

谁能给我一个例子,如何实现这一目标。 在多个标签用1单ListActivity。 2.保留在选项卡中的光标位置,而不会影响其他选项卡之后。

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.

您提供的解决方案是AP preciated。 先谢谢了。

Your solution provided is appreciated. Thanks in advance.

推荐答案

不要在列表视图调用setAdapter()了。做这样的事情。

Don't call setAdapter() again on the list view. Do something like this.

if(myListView.getAdapter() == null)
    myListView.setAdapter(new myAdapter(this, R.layout.row, items));

如果你需要更新你的ListView电话notifyDataSetChanged()的适配器。

If you need to update your ListView call notifyDataSetChanged() on the adapter.

这篇关于如何保存和恢复在Android中的ListView位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆