如何知道 GridView 何时完全绘制并准备就绪? [英] How to know when GridView is completely drawn and ready?

查看:16
本文介绍了如何知道 GridView 何时完全绘制并准备就绪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义适配器的长 GridView,我如何知道 GridView 何时完全显示并准备就绪?

I have a long GridView with custom adapter, How do I know when GridView is completely displayed and ready?

这是我的代码问题:

dashboard = (GridView) findViewById(R.id.dashboard);
dashboard.setAdapter(new ListItemsAdapter(this, allIcons));
AlertSomeItemsOfTheListView();

在 GridView 完全绘制之前,按顺序执行方法AlertSomeItemsOfTheListView".

in the sequence the method "AlertSomeItemsOfTheListView" is executed before the GridView is completely drawn.

推荐答案

你可以获得 ViewTreeObserver 实例,用于您的 GridView 并使用它来侦听诸如 onLayout 之类的事件.您可以使用 View.getViewTreeObserver().我不确定 onLayout 事件是否足以满足您的需求,因为它并不完全意味着 View 已完全绘制,但您可以尝试一下.

You could get ViewTreeObserver instance for your GridView and use it to listen for events such as onLayout. You can get ViewTreeObserver for your View using View.getViewTreeObserver(). I am not sure if onLayout event will be enough for you as it does not exactly mean that a View is fully drawn but you can give it a try.

这里有一个代码示例,你可以用它来监听 onLayout 事件(你可以在 ActivityonCreate 方法中使用这样的代码代码>):

Here there is a code sample which you could use to listen for onLayout event (you could use such code e.g. in onCreate method of your Activity):

dashboard.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            AlertSomeItemsOfTheListView();

            // unregister listener (this is important)
            dashboard.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });

注意:在调用监听器后注销监听器很重要.如果您不这样做,它将在每个 onLayout 事件上调用,这不是我们在这里想要的(我们希望它只执行一次).

Note: It is important to unregister listener after it is invoked. If you don't do that it will be invoked on every onLayoutevent which is not what we want here (we want it to execute only once).

这篇关于如何知道 GridView 何时完全绘制并准备就绪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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