Android:选项卡切换 - 如何在选项卡之间切换时更新显示?

发布于 2024-11-26 18:35:55 字数 785 浏览 1 评论 0原文

我正在构建的应用程序有一个带有三个选项卡的选项卡主机。目标是在切换发生时更新新选项卡中的显示。我在构建选项卡主机的主要活动中设置了以下选项卡更改侦听器。

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
  public void onTabChanged(String tabId) {
    //Now what?
  }
});

问题是,如何获取给定的 tabId 并使用它来调用该选项卡中的方法?

编辑

为了澄清一点:当您为新选项卡创建Intent时,您指定一个活动类,大概会创建该活动类的一个对象来处理该选项卡的设置和管理。

    intent = new Intent().setClass(this, Setup.class);
    spec = tabHost.newTabSpec("setup").setIndicator("",
      res.getDrawable(R.drawable.tab_setup))
        .setContent(intent);
    tabHost.addTab(spec);

我在这个问题中寻找的是如何获取该对象的引用?在上面的示例中,Setup 类被实例化以处理“Setup Tab”。

现在重申我的问题:如何从 OnTabChangeListener 调用 Setup 类中的方法?

The app I am building has a tabhost with three tabs. The goal is to update the display in the new tab when the switch takes place. I have the following tab change listener set up in the main activity that built the tab host.

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
  public void onTabChanged(String tabId) {
    //Now what?
  }
});

The question is, How do I take the tabId given and use it to call a method in that tab?

Edit

To clarify a bit: when you create an Intent for a new tab you specify an Activity Class, an object of which, presumably, is created to handle setup and management of that tab.

    intent = new Intent().setClass(this, Setup.class);
    spec = tabHost.newTabSpec("setup").setIndicator("",
      res.getDrawable(R.drawable.tab_setup))
        .setContent(intent);
    tabHost.addTab(spec);

What I am looking for with this question is how to get a reference to that object? In the example above, the Setup class is instantiated to handle the “Setup Tab”.

To restate my question now: How do I, from the OnTabChangeListener, call a method in the Setup class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你另情深 2024-12-03 18:35:55

尝试使用

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
        public void onTabChanged(String tabId) {
            //Now what?
                  Object.refreshDrawableState();
        }
        });

Where Object = 任何显示对象。

Try to use

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
        public void onTabChanged(String tabId) {
            //Now what?
                  Object.refreshDrawableState();
        }
        });

Where Object = any display Object.

冬天旳寂寞 2024-12-03 18:35:55

事实证明,问题的解决方案离我们家更近了。该解决方案不是将代码附加到 TabHost,而是通过重写 onPause 和 onResume 方法进入活动的生命周期。

@Override
public void onResume() {
    super.onResume();
    // Update your UI here.
}

As it turns out the solution to the problem is closer to home. Rather than attaching code to the TabHost, the solution involves getting into the Life-Cycle of the activity by overriding the onPause and onResume methods.

@Override
public void onResume() {
    super.onResume();
    // Update your UI here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文