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.
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 onLayout
event which is not what we want here (we want it to execute only once).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With