< - 抽屉图标
如果你使用android studio创建一个带导航抽屉的项目,当你打开/关闭抽屉时,抽屉图标会有一个平滑的动画。
如果我在抽屉布局中添加一个抽屉监听器,抽屉图标就不再有动画了:
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));
我试图覆盖ActionBarDrawerToggle的方法来添加对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);
现在,当抽屉打开时,我有一个小图标,然后在抽屉关闭时切换到一个大图标,但我没有平滑的动画。
有人知道如何获得流畅的动画吗?
答案 0 :(得分:1)
尝试放置最后两个侦听器缺少的@Override
注释,并删除所有syncState()
个调用。从您的活动onPostCreate
:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
答案 1 :(得分:1)
我遇到了同样的问题。问题问题已经很长时间了,但如果你还有兴趣,这是我的回答。
把
invalidateOptionsMenu();
而不是
syncstate();
for onDrawerClosed(),onDrawerOpened(),onDrawerSlide(),onDrawerStateChanged()
另外,把
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
答案 2 :(得分:0)
将布尔值初始化为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();
}
};