Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when GridView is completely drawn and ready?

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

Here's my problem in code:

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

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

like image 347
Shehabic Avatar asked Jan 02 '13 08:01

Shehabic


1 Answers

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.

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);
        }
    });

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).

like image 65
Piotr Avatar answered Nov 15 '22 05:11

Piotr