【问题标题】:Android - onTabChanged not being invoked upon selecting a different tabAndroid - 选择不同的选项卡时不会调用 onTabChanged
【发布时间】:2011-09-26 04:23:54
【问题描述】:

感谢您查看我的询问!

我已经实现了一个选项卡环境,如下图所示。它似乎按我的预期工作,只是它从不执行 onTabChanged 方法。我发现了几篇关于这种情况的帖子,但无法让我的代码按我预期的方式工作。建议?

谢谢, 芯片

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

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    我没有看到您使用tabHost.setOnTabChangeListener(this) 注册您的课程以进行标签更改。

    我建议在tabHost.setCurrentTab(0) 之前这样做。

    即使没有在第一组选项卡上调用它,您也可以在onCreate 末尾手动调用onTabChanged("page1")

    还是我误解了你的问题?

    【讨论】:

    • 谢谢尼克迪!我是瞎子,但现在我明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多