我有一个带有导航抽屉的活动,它可以正常工作。唯一的问题是汉堡菜单在第一个片段替换后没有动画。
public class Main extends AppCompatActivity {
// lots of attributes
@Override
protected void onCreate(Bundle in) {
super.onCreate(in);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
setupDrawer();
}
public void onEventMainThread(LoadedLEsEvent event) {
setupDrawer();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
void setupDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, navDrawer, R.string.drawer_open, R.string.drawer_closed) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
drawerToggle.setDrawerIndicatorEnabled(true);
navDrawer.setDrawerListener(drawerToggle);
menuListAdapter = new MenuListAdapter(this, R.layout.drawer_item, someList);
menuListView.setAdapter(menuListAdapter);
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Replaces the frame layout with a fragment
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
所以基本上它工作得很好,直到我用一个片段替换FrameLayout。从这里开始,抽屉仍然可以工作,但汉堡菜单根本没有动画。
所以问题是每次调用setupDrawer()时都初始化了drawerToggle
。将drawerToggle = new ActionBarDrawerToggle(...) {...};
移动到onCreate()
就成功了。