Android的 - 如何判断当特定视图已经被渲染? [英] Android - How to tell when a specific view has been rendered?

查看:313
本文介绍了Android的 - 如何判断当特定视图已经被渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定期的按钮,我想显示一个计时器。所以,想象一下按钮的文本改变每一秒的倒计时器。不过,我需要这个定时器只启动按钮时,完全呈现,并准备去在屏幕上。

I have a regular button that I want to display a timer on. So imagine the text of the button changes every second as a countdown timer. However, I need this timer to only start when the button is fully rendered and ready to go on the screen.

现在,我启动定时器(这是一个的AsyncTask )的 onCreateView()的分段。这不是准确的,因为我做一些其他装载的东西,我必须有。我可能会在底部移动定时启动 onCreateView()但即使要么不是很准确。

Right now I'm starting the timer (which is an AsyncTask) in the onCreateView() of a fragment. This isn't that accurate, because I do some other loading stuff that I have to have. I could potentially move the timer start at the bottom of onCreateView() but even that isn't very accurate either.

我看到有一个 OnGlobalLayoutListener 树观察者。但我想这时候整个视图树即将被抽?我怎么知道什么时候我的特定按钮呈现和对用户可见?

I saw that there's a OnGlobalLayoutListener tree observer. But I imagine this when an entire view tree is "about to be" drawn? how do I know exactly when my particular button is rendered and visible to the user?

推荐答案

OnGlobalLayoutListener()是去正确的地方。通过使用它,它是保证它会被称为当你的按钮已经奠定了(完全渲染,并准备去在屏幕上)。例如,在OnCreate()中,你可以这样做:

OnGlobalLayoutListener() is the right place to go. By using it it's guaranteed that it will be called when your button has been layed out ("fully rendered and ready to go on the screen"). For example, in onCreate() you could do:

    btn = (Button) findViewById(R.id.bn);   
    btn.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

            new AsyncTaskTimer.execute();

        }
    });

如果您不希望不止一次添加以下行onGlobalLayout监听器被称为多()方法:

If you don't want the listener to be called more than once add the following line to the onGlobalLayout() method:

    btn.getViewTreeObserver().removeOnGlobalLayoutListener(this);

这篇关于Android的 - 如何判断当特定视图已经被渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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