【问题标题】:NavigationDrawer: no more animation on drawer icon when setting drawer listenerNavigationDrawer:设置抽屉监听器时,抽屉图标上不再有动画
【发布时间】:2025-11-28 04:45:01
【问题描述】:

如果你用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);

现在,当抽屉打开时,我有一个小图标,然后在抽屉关闭时切换为大图标,但我没有流畅的动画。

有人知道如何获得流畅的动画吗?

【问题讨论】:

    标签: android animation android-actionbar navigation-drawer


    【解决方案1】:

    尝试将最后两个侦听器缺少的@Override 注释放入并删除所有syncState() 调用。从 Activity 的 onPostCreate 调用 syncState():

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }
    

    【讨论】:

    • 我刚试过,它不会改变行为:没有动画。
    【解决方案2】:

    我也遇到了同样的问题。这个问题已经很久没有问过了,如果你还有兴趣,这里是我的回答。

    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);
    }
    

    【讨论】:

      【解决方案3】:

      将布尔值设置为 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();
              }
          };
      

      【讨论】:

      • 我试过你的解决方案,但它没有解决我的问题:我没有流畅的动画(我只看到打开的抽屉图标,然后是关闭的抽屉图标,而不是动画)。
      最近更新 更多