Android的Activity.onWindowFocusChanged不会从内TabHost称为 [英] Android Activity.onWindowFocusChanged doesn't get called from within TabHost

查看:173
本文介绍了Android的Activity.onWindowFocusChanged不会从内TabHost称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挣扎了.onWindowFocusChanged()不会被调用在我的自定义Activity类的问题。我的设置:

I'm struggling with the problem that the .onWindowFocusChanged() doesn't get called in my custom Activity class. My setup:

两个选项卡(含Activity_1和Activity_2)在TabHost,其中第2个选项卡默认情况下选择:

Two tabs (containing Activity_1 and Activity_2) in a TabHost, where the 2nd tab is selected by default:

tabHost.setCurrentTab(currentTabIndex);

在这两项活动中,我添加了onWindowFocusChanged()重写(因为我需要preform计算布局完成图纸后):

In both Activities, I added the onWindowFocusChanged() override (because I need to preform calculations after the layout is done drawing):

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
}

问题:当第2个选项卡默认选中,而我点击第一个选项卡中,onWindowFocusChanged()不会被调用内Activity_1(与第1选项卡关联)。 这两项活动延长正常活动类。

The problem: when the 2nd tab is selected by default, and I click the 1st tab, the onWindowFocusChanged() never gets called within Activity_1 (associated with the 1st tab). Both Activities extend the normal Activity class.

在如何解决这一问题的任何线索将大大AP preciated!

Any clue on how to fix this would be greatly appreciated!

推荐答案

如果您需要等到特定的视图是画,然后进行计算,可以使用viewTreeObserver听布局的变化,让你的计算存在。 使用这样的:

If you need to wait until a specific View is draw and then make the calculations, you could use viewTreeObserver to listen the layout changes and make your calculations there. Use it like this:

ViewTreeObserver vto = mainLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
    // remove the listener so it won't get called again if the view layout changes
    mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    // add your calculations here
}}

我希望这是你想要的。

I hope this is what you want..

这篇关于Android的Activity.onWindowFocusChanged不会从内TabHost称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆