Android - 如何在选项卡切换后更新显示?
我有一个带有 TabHost 和三个选项卡的应用程序。他是我如何创建每个选项卡的示例:
intent = new Intent().setClass(this, Setup.class); //intent.getClass()
spec = tabHost.newTabSpec("setup").setIndicator("",
res.getDrawable(R.drawable.tab_setup))
.setContent(intent);
tabHost.addTab(spec);
我的目标是,当您切换到新选项卡时,调用更新该选项卡上显示的信息的方法。
问题是:选项卡如何知道它已经显示了?
I have an app with a TabHost and three tabs. He is an example of how I am creating each tab:
intent = new Intent().setClass(this, Setup.class); //intent.getClass()
spec = tabHost.newTabSpec("setup").setIndicator("",
res.getDrawable(R.drawable.tab_setup))
.setContent(intent);
tabHost.addTab(spec);
My goal is, when you switch to a new tab, to call a method updating the information displayed on that tab.
Here is the question: How does a tab know it has been displayed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您为
TabHost
中的每个选项卡使用Activity
,因此您应该使用 Android lifecycle 调用以更新 UI 上的信息。每次切换到另一个选项卡的
Activity
时都会调用onResume
(然后在最后一个选项卡的 Activity 中调用onPause
)。这将使您的代码保持在正确的位置,以防您决定使用不同的 UI 来托管这些活动。Since you are using an
Activity
for each tab in yourTabHost
, you should use the Android lifecycle calls to update the information on your UI.onResume
is called every time you switch to another tab'sActivity
(onPause
is then called in the last tab's activity). This will keep your code in the proper place, in case you ever decide to use a different UI to host these activities.使用所需的代码覆盖 Activity 类中的 OnTabChangeListener 。
Override OnTabChangeListener in your activity class with the desired code.
如果选项卡发生变化, onTabChangeListener 就会起作用,如果没有(单击同一个选项卡),它就不会被调用,不确定这对您的应用程序是否重要。
我想如果您愿意,您可以捕获 选项卡单击事件所有事件而不仅仅是变化。我认为第二个提议的解决方案不起作用的原因是他们没有顺便调用 super.onClick(event) 。
onTabChangeListener works if the tab changes, if it does not (you click on the same tab) it won't get called, not sure if that is important or not to your application.
I suppose you could capture the tab click event if you want all events and not just changes. I think the reason the 2nd proposed solution is not working is they are not calling super.onClick(event) by the way.
我认为使用选项卡的目的是在第一次单击时加载选项卡,然后保持静态,直到用户发出刷新命令。我会在菜单中使用一个新鲜按钮。
但是,如果您需要在每次单击选项卡时更新选项卡,那么为什么不只使用几个简单的按钮呢?单击它们后,您可以轻松交换内容区域。
I think the purpose of using tabs is to load a tab on the first click and then stay static until a user issues a refresh command. I would use a fresh button in the menu.
But if you need to update a tab every time it is clicked, then why not just use a few simple buttons? When they are clicked, you can swap your content areas easily.