开发者

Android - onTabChanged not being invoked upon selecting a different tab

Thanks for checking out my inquiry!

I have implemented a tab environment as illustrated below. It seems to work as I expected except that it never executes the onTabChanged method. I have found several posts about this type of situation but have not been able to get my code to work the way I expected. Advice?

Thanks, Chip

public class TestTabActivity extends TabActivity implements OnTabChangeListener {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    intent = new Intent().setClass(this, Page1Activity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    spec = tabHost.newTabSpec("page1").setIndicator("Page 1",
                      res.getDrawable(R.drawable.ic_tab_page1))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Page2Activity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec = tabHost.newTabSpec("page2").setIndicator("Page 2",
                      res.getDrawable(R.drawable.ic_tab_page2))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Page2Activity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec = tabHost.newTabSpec("page3").setIndicator("Page 3",
                      res.ge开发者_如何学运维tDrawable(R.drawable.ic_tab_page3))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}   

public void onTabChanged(String tabId) {
    Log.d("Tab Changed", "Changed a Tab");
}
}   


I don't see that you register your class for a tab change with tabHost.setOnTabChangeListener(this).

I would suggest to do that before tabHost.setCurrentTab(0).

Even if it's not called on the first set of the tab you can manually call onTabChanged("page1") at the end of onCreate.

Or did I missunderstood your problem?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜