8

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.

Shehabic
  • 6,787
  • 9
  • 52
  • 93
  • oh ! BTW how long the listview is? – Kalpesh Lakhani Jan 02 '13 at 08:50
  • You don't. The ListView will ask the adapter only for those items that are currently showing. – Vinay S Shenoy Jan 02 '13 at 08:50
  • The problem is that I want to alter the content of the ListView using external method, but when I write them in 2 following lines the 2nd line is executed before the listView is complete drawn. – Shehabic Jan 02 '13 at 08:52
  • @KalpeshLakhani It's actually a gridview with 4 rows and 4 columns, But the code in getView in the adapter consumes sometime cause it puts external reference to TextView and ImageView in the ListItem (To be used by the external Method for altering its content later). – Shehabic Jan 02 '13 at 08:56
  • ok ! buddy understood your problem. and yes critical it is. By the way try lazy loading for this case if you comfort with. – Kalpesh Lakhani Jan 02 '13 at 09:11
  • I found a work around "The Polling Way" (the last item that I would want to alter toggles a public boolean when it's called in getView), and my other method sleeps 100 ms and checks for this boolean, once it the bolean is toggled, it starts changing the content of the layout. – Shehabic Jan 02 '13 at 09:44

2 Answers2

18

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

dragi
  • 3,385
  • 5
  • 22
  • 40
Piotr
  • 5,543
  • 1
  • 31
  • 37
  • 2
    For V13 + "removeGlobalOnLayoutListener" is deprecated. Use removeOnGlobalLayoutListener instead. – averydev Oct 03 '13 at 18:46
0

When a GridView/ListView has an adapter, it calls getView when it displays items that are to be in view. If you REALLY needed to call a method after all Views are shown, you could do something like....

while(!dashboard.getAdapter().areAllItemsEnabled()){
   //wait
}

AlertSomeItemsOfTheListView();

I'm not positive that this will work, but looking at http://developer.android.com/reference/android/widget/GridView.html#getAdapter() and http://developer.android.com/reference/android/widget/ListAdapter.html make it seem like it will.

snotyak
  • 3,709
  • 6
  • 35
  • 52
  • I found a work around similar to your answer "The Polling Way" (the last item that I would want to alter toggles a public boolean when it's called in getView), and my other method sleeps 100 ms and checks for this boolean, once it the bolean is toggled, it starts changing the content of the layout – Shehabic Jan 02 '13 at 09:44