Android - 从选项卡内的活动中切换选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2541802/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:15:52  来源:igfitidea点击:

Android - Switch Tabs from within an Activity within a tab

androidtabsandroid-intent

提问by crv

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.

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

回答by crv

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.

在与这个问题斗争了很长时间后,我已经能够找到在使用基于活动的标签时切换标签的解决方案。

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

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

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);
}

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

如果您想要此代码的一个很好的示例,您可以在此处此处查看我的 MintTrack 项目。

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

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

Activitybased 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. Activitybased tabs also use more memory/CPU time as they have the overhead of the Activityaround each of them. Please consider this and the many more trade offs before diving into using an Activitybased TabHost. I know now that I would personally go with a view based TabHostif I ever used them again.

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

回答by Anders Petersson

I encountered the same problem. While a single activity for all tabs would be better, sometimes taking the easy way out is the rational choice.

我遇到了同样的问题。虽然所有选项卡的单一活动会更好,但有时采取简单的方法是合理的选择。

To avoid creating a new tab activity when a tab wants to change to another tab, I put this in my AndroidManifest.xml:

为了避免在一个选项卡想要更改为另一个选项卡时创建新的选项卡活动,我将其放在我的 AndroidManifest.xml 中:

<activity android:name=".MyTabsActivity"
        android:label="Tabs!"
        android:launchMode="singleTask">

Send an intent with the tab you want:

使用您想要的选项卡发送意图:

class OneTabContentActivity {
  void switchTab() {
    final Intent intent = new Intent(mContext, MyTabsActivity.class);
    intent.setAction("Switch to tab 1, please");
    mContext.startActivity(intent);
}

class MyTabsActivity {
  @Override
  protected void onNewIntent (Intent intent) {
    super.onNewIntent(intent);
    getTabHost().setCurrentTab(1);
  }
}

This solution has drawbacks but I'm not clear over the details. Someone else might know enough to point them out.

这个解决方案有缺点,但我不清楚细节。其他人可能知道的足够多来指出它们。

回答by Sertalp Bilal

First, I set a method to my main class, which extends TabActivity let's call it "MainActivity"

首先,我为我的主类设置了一个方法,它扩展了 TabActivity 让我们称之为“MainActivity”

public TabHost getMyTabHost() { return tabHost; }

Then, I add my tab activity class;

然后,我添加我的选项卡活动类;

MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);

It worked for me.

它对我有用。

回答by CommonsWare

Step #1: Replace the tabs-holding-activities with tabs-holding-views by using a better form of setContent()on TabSpec

第 1 步:使用更好的setContent()on形式将 tabs-holding-activity 替换为 tabs-holding-viewsTabSpec

Step #2: Call setCurrentTab()on your TabHostfrom within your single Activity

第2步:呼叫setCurrentTab()TabHost从内您的单Activity

I have yet to see any benefit to having an Activitybe the content of a tab rather than a simple View. Having an Activityas the content of the tab wastes CPU time and memory (and, hence, battery life) and makes things like you're trying to do much more difficult.

我还没有看到Activity成为选项卡的内容而不是简单的View. 拥有Activity作为选项卡的内容会浪费 CPU 时间和内存(以及电池寿命),并使您尝试做的事情变得更加困难。

回答by bkurzius

I had a slightly different problem and thought I'd add this for anyone else facing a similar situation. I have an activity-based tabbed application and one of the tab activities launches another activity which is not controlled by the tabHost. I needed to have a button on this activity finish() (ie: return back to the main Tab view) and switch to a different tab at the same time.

我有一个稍微不同的问题,并认为我会为其他面临类似情况的人添加这个。我有一个基于活动的选项卡式应用程序,其中一个选项卡活动启动了另一个不受 tabHost 控制的活动。我需要在此活动完成()上有一个按钮(即:返回主选项卡视图)并同时切换到不同的选项卡。

I decided to handle it with a BroadcastReceiver. In the class that sets up the TabHost, I added this class:

我决定用一个 BroadcastReceiver 来处理它。在设置 TabHost 的类中,我添加了这个类:

class ChangeTabReceiver extends BroadcastReceiver { 
   @Override 
   public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "ChangeTabReceiver: received");
     TabHost tabHost = getTabHost();
     tabHost.setCurrentTab(1);
   } 
}

..then defined the vars:

..然后定义变量:

ChangeTabReceiver changeTabReceiver;
IntentFilter changeTabFilter;

..then added to onCreate():

..然后添加到 onCreate():

changeTabReceiver = new ChangeTabReceiver();
changeTabFilter = new IntentFilter(myApplication.CHANGE_TAB); 
registerReceiver(changeTabReceiver, changeTabFilter);

Finally in the new activity when you want to close that activity and switch the tabs, do this:

最后在新活动中,当您要关闭该活动并切换选项卡时,请执行以下操作:

Intent intent = new Intent(myApplication.CHANGE_TAB);
this.sendBroadcast(intent); 
this.finish();

Of course you could make a method to switch to various tabs by passing the tab index -- but in my case this behavior only occurs in one activity so I decided to keep it simple...

当然,您可以通过传递标签索引来制作一种切换到各种标签的方法——但在我的情况下,这种行为只发生在一个活动中,所以我决定保持简单......

回答by Tim

I just put a public static TabHost tabHost;in my TabActivity.

我只是public static TabHost tabHost;在我的TabActivity.

Then from any other tab I can do a MyTabActivity.tabHost.setCurrentTab(tabNumber);

然后从任何其他选项卡我可以做 MyTabActivity.tabHost.setCurrentTab(tabNumber);

Works fine for me (but I wish I'd used Fragments from the start.. I was just following the Tab tutorial in the Android documentation and working from there)

对我来说很好用(但我希望我从一开始就使用 Fragments ..我只是按照 Android 文档中的 Tab 教程并从那里开始工作)

回答by Jimmy Rollins

public void switchTab(int index){
   MintTrack ParentActivity;
   ParentActivity = (MintTrack) this.getParent();
   ParentActivity.getTabHost().setCurrentTab(index);
}