从 Android TabHost 中的选项卡活动更新选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update tabs from a tab's activity in a Android TabHost
提问by Josh Lillie
I have a TabActivity and am going to have 3 or 4 tabs.
我有一个 TabActivity 并且将有 3 或 4 个选项卡。
In TabA, I would like to have an ExpandableListView, which will list products. I would like TabC to act as a sort of "shopping cart", probably with a ListView of itself.
在 TabA 中,我想要一个 ExpandableListView,它将列出产品。我希望 TabC 充当一种“购物车”,可能带有自己的 ListView。
Is there a way to interact with the TabHost from the Activity? I would like to be able to click a button inside of TabA and have it update and switch to TabC. Is this possible? Can I get the Tab's activities call its parents methods?
有没有办法从 Activity 与 TabHost 交互?我希望能够单击 TabA 内的按钮并更新它并切换到 TabC。这可能吗?我可以让 Tab 的活动调用其父方法吗?
回答by CommonsWare
If you implement your tabs as views, rather than individual activities, this would be far simpler, run faster, take up less memory, and be a better solution IMHO.
如果您将选项卡实现为视图,而不是单个活动,这将更简单,运行速度更快,占用内存更少,并且是更好的解决方案恕我直言。
That being said, in the Tab A Activity
, call getParent()
to get at the TabActivity
. Then, you can use the methods pointed out by disretrospect.
话虽如此,在 Tab A 中Activity
,调用getParent()
以获取TabActivity
. 然后,您可以使用disretrospect 指出的方法。
回答by Randy V
Try to use this code to change activity within a tab:
尝试使用此代码更改选项卡内的活动:
TabActivity parent = (TabActivity) getParent();
TabHost tabhost = parent.getTabHost();
tabhost.setCurrentTab(0);
回答by m6tt
You can call getTabHost from a TabActivity, see public TabHost getTabHost().
您可以从 TabActivity 调用 getTabHost,请参阅public TabHost getTabHost()。
See Tabhost for setCurrentTab, public void setCurrentTab(int index).
有关 setCurrentTab 的信息,请参阅 Tabhost,public void setCurrentTab(int index)。
回答by Hyman Cheung
- In your Activity, use getParent() to get the TabActivity parent
- In your TabActivity class, use getLocalActivityManager().getActivity(tabId) to get the Activity object
- call the Activity object's method
- 在您的 Activity 中,使用 getParent() 获取 TabActivity 父级
- 在您的 TabActivity 类中,使用 getLocalActivityManager().getActivity(tabId) 获取 Activity 对象
- 调用 Activity 对象的方法