java Android Tabhost 刷新

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

Android Tabhost refresh

javaandroidandroid-tabhost

提问by thathashd

I have the following code to tabhost.

我有以下代码来tabhost。

tabHost.addTab(tabHost.newTabSpec("tab1")
            .setIndicator("First Text")
            .setContent(new Intent(this, class1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
            .setIndicator("Second Text")
            .setContent(new Intent(this, class2.class)));

How can i refresh each tab? I want to do this because i have some problems showing data from database.

如何刷新每个选项卡?我想这样做是因为我在显示数据库中的数据时遇到了一些问题。

tanks

坦克

回答by Tanmay Mandal

I think this is what you want

我想这就是你想要的

 tabHost.addTab(tabHost.newTabSpec("tab1")
        .setIndicator("First Text")
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        .setContent(new Intent(this, class1.class)));
 tabHost.addTab(tabHost.newTabSpec("tab2")
        .setIndicator("Second Text")
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        .setContent(new Intent(this, class2.class)));

Just use .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)to your tab class and get your desired result

只需使用.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)您的选项卡类并获得您想要的结果

回答by Kun Andrei

Just use onResumefunction. For me works just fine.

就用onResume函数吧。对我来说效果很好。

回答by Kartik Domadiya

Use http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html

使用http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html

For Example :

例如 :

  mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
  @Override
 public void onTabChanged(String tabId) {
    if(TAB_1.equals(tabId)) {
     //Do first activity task
  }
  if(TAB_2.equals(tabId)) {
      //Do the other task...so on
  }
}});

Edited :

编辑:

If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.

如果您想看到某个特定的选项卡被点击,您需要将您的侦听器添加到选项卡本身,而不是 TabHost。

The hierarchy of views in a tab implementation is:

选项卡实现中的视图层次结构是:

TabHost
    TabWidget
        (tab)
        (tab)
    FrameLayout

The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));

通过调用在运行时添加选项卡: tabHost.addTab(tabHost.newTabSpec(""));

You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);

然后您可以通过调用获取单个选项卡的句柄:getTabWidget().getChildAt(4);

In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.

本质上,您将 OnClickListener 添加到 TabWidget 的子项。您现在可以在您的个人标签上获取点击次数。但是,这将覆盖单击选项卡时更改内容的默认行为。因此,要更改您的内容,您的 OnClickListener 将需要为您执行此操作。

Here is a full example, which lets you intercept the click event, and change the content below the tab:

这是一个完整的示例,它可以让您拦截单击事件,并更改选项卡下方的内容:

 final String myTabTag = "My Tab";
 final int myTabIndex = 3;

 getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );

 getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
     if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
        getTabHost().setCurrentTab(myTabIndex );
    }
}
});

回答by Harinder

public static void chngtab(String name) {
    ((TextView)tabHost.getTabWidget().getChildAt(0)
                      .findViewById(android.R.id.title)).setText(name);
}