开发者

Android Tabhost refresh

I have the following code to tabhost.

tabHost.addTab(tabHost.newTabSpec("tab1")
            .setIndicator(开发者_如何学JAVA"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


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


Just use onResume function. For me works just fine.


Use 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.

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(""));

You can then get a handle to the individual tabs by calling: 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.

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜