Android - 从选项卡内的活动中切换选项卡 [英] Android - Switch Tabs from within an Activity within a tab

查看:43
本文介绍了Android - 从选项卡内的活动中切换选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个 TabHost,其中包含 3 个选项卡,每个选项卡都包含一个单独的活动.我的问题是如何从位于选项卡主机内的活动之一中的选项卡之间切换.我到处寻找,但没有成功找到这个问题的真正答案.

Currently I have a TabHost implemented with 3 tabs each containing a separate activity. My question is how do I switch between tabs from within one of the activities that is located inside the tab host. I've looked everywhere and have been unsuccessful in finding a real answer to this problem.

推荐答案

在与这个问题斗争了很长时间后,我找到了在使用基于活动的选项卡时切换选项卡的解决方案.

After a long time of battling with this problem I've been able to find a solution to switching tabs when using activity based tabs.

在创建 tabhost 的父活动类中,我实现了如下所示的方法:

In the parent activity class where the tabhost is created I implemented a method like the one below:

public void switchTab(int tab){
            tabHost.setCurrentTab(tab);
}

在我希望能够在内部切换到另一个选项卡的选项卡内,我创建了以下方法:

Inside of the tab that I would like to be able to switch internally to another tab I created the method below:

public void switchTabInActivity(int indexTabToSwitchTo){
            MintTrack parentActivity;
            parentActivity = (MintTrack) this.getParent();
            parentActivity.switchTab(indexTabToSwitchTo);
}

如果你想要这个代码的一个很好的例子,你可以看看我的 MintTrack 项目 这里这里.

If you would like a good example of this code, you can take a look at my MintTrack project here and here.

作为旁注,在决定是使用基于视图还是基于活动的 TabHost 时请务必小心.

As a side note, please be very careful when deciding whether to use view or activity based TabHost.

Activity 的选项卡很棒,因为它们可以分离到自己的 XML 文件中.活动也可以组织到自己的 Java 文件中,而不是杂乱无章.话虽如此,您认为很容易的一些事情会因基于活动的选项卡而变得复杂.很难在选项卡之间传递信息而不产生开销.基于 Activity 的选项卡也使用更多的内存/CPU 时间,因为它们在每个选项卡周围都有 Activity 的开销.在深入使用基于 ActivityTabHost 之前,请考虑这一点以及更多的权衡.我现在知道,如果我再次使用它们,我会亲自使用基于视图的 TabHost.

Activity based tabs are great because they can be separated into there own XML file. Activities can also be organized into there own Java file instead of being cluttered into one. That being said some of the things you would think would be easy become complicated with activity based tabs. Its hard to pass information between tabs without creating overhead. Activity based tabs also use more memory/CPU time as they have the overhead of the Activity around each of them. Please consider this and the many more trade offs before diving into using an Activity based TabHost. I know now that I would personally go with a view based TabHost if I ever used them again.

这篇关于Android - 从选项卡内的活动中切换选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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