如何知道什么时候GridView控件的绘制完成,并准备好了? [英] How to know when GridView is completely drawn and ready?
问题描述
我还有很长的GridView自定义适配器,我怎么知道什么时候GridView中完全显示出来,并准备好了?
I have a long GridView with custom adapter, How do I know when GridView is completely displayed and ready?
下面是我在code问题:
Here's my problem in code:
dashboard = (GridView) findViewById(R.id.dashboard);
dashboard.setAdapter(new ListItemsAdapter(this, allIcons));
AlertSomeItemsOfTheListView();
序列中的方法AlertSomeItemsOfTheListView被执行之前将GridView被完全抽出。
in the sequence the method "AlertSomeItemsOfTheListView" is executed before the GridView is completely drawn.
推荐答案
您可以得到的 ViewTreeObserver > <$ C $ 实例为您的GridView
,并用它来收听,如 onLayout
。你可以得到 ViewTreeObserver
您查看
使用的 View.getViewTreeObserver()
。我不知道,如果 onLayout
活动将是够你,因为它并不完全意味着一个查看
完全画但你可以试试看。
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.
下面有一个code样品,你可以用它来听 onLayout
事件(你可以使用这样的code例如,在你的
):活动中的onCreate
方法
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 onLayout
event which is not what we want here (we want it to execute only once).
这篇关于如何知道什么时候GridView控件的绘制完成,并准备好了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!