Reputation: 425
<-- the drawer icon
If you create, with android studio, a project with a navigation drawer, when you open/close the drawer, there will be a smooth animation of the drawer icon.
If I add a drawer listener to my drawerlayout, there is no more animation the drawer icon doesnt change anymore:
DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawer_layout);
dl.setDrawerListener(new ActionBarDrawerToggle(this, dl,R.drawable.ic_drawer,R.string.navigation_drawer_open,R.string.navigation_drawer_close));
I tried to override methods of ActionBarDrawerToggle to add calls to syncState().
DrawerLayout dl = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, dl,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
mDrawerToggle.syncState();
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
mDrawerToggle.syncState();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
mDrawerToggle.syncState();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
mDrawerToggle.syncState();
}
};
dl.setDrawerListener(mDrawerToggle);
Now, when the drawer is open, I have a small icon, and then it switch to a large icon when the drawer is closed, but I dont have the smooth animation.
Does somebody know how to get the smooth animation?
Upvotes: 2
Views: 1527
Reputation: 11
I have had the same problem.It's been a long time since the question was asked, but if you are still interested, here is my answer.
put
invalidateOptionsMenu();
instead of
syncstate();
for onDrawerClosed(), onDrawerOpened(), onDrawerSlide(), onDrawerStateChanged()
Also, put
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
Upvotes: 1
Reputation: 11316
Intilize the boolean as false..private boolean isDrawerOpen = false;
public void onDrawerSlide(View drawerView, float slideOffset) {
if(slideOffset > .55 && !isDrawerOpen){
super.onDrawerSlide(drawerView, 1);
onDrawerOpened(drawerView);
isDrawerOpen = true;
} else if(slideOffset < .45 && isDrawerOpen) {
super.onDrawerSlide(drawerView,slideOffset);
onDrawerClosed(drawerView);
isDrawerOpen = false;
}
}
public void onDrawerClosed(View view) {
getActionBar().setTitle("Your title");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("your title");
invalidateOptionsMenu();
}
};
Upvotes: 0
Reputation: 391
Try to put the @Override
annotations that are missing for the last two listeners and remove all syncState()
calls. Call syncState() from your Activity's onPostCreate
:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
Upvotes: 1