片段更换后抽屉切换不动画
Drawer toggle not animating after Fragment replacement
我有一个 activity 带有导航抽屉,可以正常工作。唯一的问题是汉堡包菜单在第一次片段替换后没有动画。
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()
就成功了。
我有一个 activity 带有导航抽屉,可以正常工作。唯一的问题是汉堡包菜单在第一次片段替换后没有动画。
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()
就成功了。