Android - 如何在选项卡切换后更新显示?

发布于 2024-11-27 00:39:55 字数 404 浏览 3 评论 0原文

我有一个带有 TabHost 和三个选项卡的应用程序。他是我如何创建每个选项卡的示例:

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

我的目标是,当您切换到新选项卡时,调用更新该选项卡上显示的信息的方法。

问题是:选项卡如何知道它已经显示了?

I have an app with a TabHost and three tabs. He is an example of how I am creating each tab:

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

My goal is, when you switch to a new tab, to call a method updating the information displayed on that tab.

Here is the question: How does a tab know it has been displayed?

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

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

发布评论

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

评论(4

思慕 2024-12-04 00:39:55

由于您为 TabHost 中的每个选项卡使用 Activity,因此您应该使用 Android lifecycle 调用以更新 UI 上的信息。

每次切换到另一个选项卡的 Activity 时都会调用 onResume(然后在最后一个选项卡的 Activity 中调用 onPause)。这将使您的代码保持在正确的位置,以防您决定使用不同的 UI 来托管这些活动。

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

Since you are using an Activity for each tab in your TabHost, you should use the Android lifecycle calls to update the information on your UI.

onResume is called every time you switch to another tab's Activity (onPause is then called in the last tab's activity). This will keep your code in the proper place, in case you ever decide to use a different UI to host these activities.

@Override
public void onResume() {
    super.onResume();
    // Update your UI here.
}
中二柚 2024-12-04 00:39:55

使用所需的代码覆盖 Activity 类中的 OnTabChangeListener

Override OnTabChangeListener in your activity class with the desired code.

浪漫之都 2024-12-04 00:39:55

如果选项卡发生变化, onTabChangeListener 就会起作用,如果没有(单击同一个选项卡),它就不会被调用,不确定这对您的应用程序是否重要。

我想如果您愿意,您可以捕获 选项卡单击事件所有事件而不仅仅是变化。我认为第二个提议的解决方案不起作用的原因是他们没有顺便调用 super.onClick(event) 。

onTabChangeListener works if the tab changes, if it does not (you click on the same tab) it won't get called, not sure if that is important or not to your application.

I suppose you could capture the tab click event if you want all events and not just changes. I think the reason the 2nd proposed solution is not working is they are not calling super.onClick(event) by the way.

旧城烟雨 2024-12-04 00:39:55

我认为使用选项卡的目的是在第一次单击时加载选项卡,然后保持静态,直到用户发出刷新命令。我会在菜单中使用一个新鲜按钮。

但是,如果您需要在每次单击选项卡时更新选项卡,那么为什么不只使用几个简单的按钮呢?单击它们后,您可以轻松交换内容区域。

I think the purpose of using tabs is to load a tab on the first click and then stay static until a user issues a refresh command. I would use a fresh button in the menu.

But if you need to update a tab every time it is clicked, then why not just use a few simple buttons? When they are clicked, you can swap your content areas easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文