Android:选项卡切换 - 如何在选项卡之间切换时更新显示?
我正在构建的应用程序有一个带有三个选项卡的选项卡主机。目标是在切换发生时更新新选项卡中的显示。我在构建选项卡主机的主要活动中设置了以下选项卡更改侦听器。
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
}
});
问题是,如何获取给定的 tabId
并使用它来调用该选项卡中的方法?
编辑
为了澄清一点:当您为新选项卡创建Intent
时,您指定一个活动类,大概会创建该活动类的一个对象来处理该选项卡的设置和管理。
intent = new Intent().setClass(this, Setup.class);
spec = tabHost.newTabSpec("setup").setIndicator("",
res.getDrawable(R.drawable.tab_setup))
.setContent(intent);
tabHost.addTab(spec);
我在这个问题中寻找的是如何获取该对象的引用?在上面的示例中,Setup
类被实例化以处理“Setup Tab”。
现在重申我的问题:如何从 OnTabChangeListener
调用 Setup
类中的方法?
The app I am building has a tabhost with three tabs. The goal is to update the display in the new tab when the switch takes place. I have the following tab change listener set up in the main activity that built the tab host.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
}
});
The question is, How do I take the tabId
given and use it to call a method in that tab?
Edit
To clarify a bit: when you create an Intent
for a new tab you specify an Activity Class, an object of which, presumably, is created to handle setup and management of that tab.
intent = new Intent().setClass(this, Setup.class);
spec = tabHost.newTabSpec("setup").setIndicator("",
res.getDrawable(R.drawable.tab_setup))
.setContent(intent);
tabHost.addTab(spec);
What I am looking for with this question is how to get a reference to that object? In the example above, the Setup
class is instantiated to handle the “Setup Tab”.
To restate my question now: How do I, from the OnTabChangeListener
, call a method in the Setup
class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
Where Object = 任何显示对象。
Try to use
Where Object = any display Object.
事实证明,问题的解决方案离我们家更近了。该解决方案不是将代码附加到 TabHost,而是通过重写 onPause 和 onResume 方法进入活动的生命周期。
As it turns out the solution to the problem is closer to home. Rather than attaching code to the TabHost, the solution involves getting into the Life-Cycle of the activity by overriding the onPause and onResume methods.