Your problem is that the onDrawerOpened()
method doesn't fire until the drawer is completely open. The perceived delay is due to the View
being visible during the opening.
One solution would be to disable dragging the drawer, and only allow the toggle to open and close it. You could then do your size check before opening it programmatically. This, though, would require locking the drawer to the appropriate state in each of the onDrawerOpened()
and onDrawerClosed()
methods, and, of course, you'd lose that standard mode of interaction.
The preferred option is probably to do the check and visibility setting as soon as possible as the drawer is starting to open. We can do this in the onDrawerSlide()
method instead, keeping a boolean
flag to determine if the drawer is sliding after having been closed. For example:
actionBarDrawerToggle = new ActionBarDrawerToggle(...) {
private boolean opened;
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if (slideOffset == 0) {
opened = false;
}
else {
if (!opened) {
opened = true;
if (items.size() == 0) {
view.setVisibility(View.GONE);
}
else {
view.setVisibility(View.VISIBLE);
}
}
}
}
...
}