吃鸡游戏
我为EndDrawerToggle该类编写了一个与您的设置非常相似的设置- DrawerLayout带末端对齐的抽屉View,AppCompatActivity带有自定义Toolbar的支持ActionBar。import android.app.Activity;import android.support.v4.view.GravityCompat;import android.support.v4.widget.DrawerLayout;import android.support.v7.graphics.drawable.DrawerArrowDrawable;import android.support.v7.widget.AppCompatImageButton;import android.support.v7.widget.Toolbar;import android.support.v7.widget.Toolbar.LayoutParams;import android.view.View;import android.view.View.OnClickListener;public class EndDrawerToggle implements DrawerLayout.DrawerListener { private DrawerLayout drawerLayout; private DrawerArrowDrawable arrowDrawable; private AppCompatImageButton toggleButton; private String openDrawerContentDesc; private String closeDrawerContentDesc; public EndDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) { this.drawerLayout = drawerLayout; this.openDrawerContentDesc = activity.getString(openDrawerContentDescRes); this.closeDrawerContentDesc = activity.getString(closeDrawerContentDescRes); arrowDrawable = new DrawerArrowDrawable(toolbar.getContext()); arrowDrawable.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_END); toggleButton = new AppCompatImageButton(toolbar.getContext(), null, R.attr.toolbarNavigationButtonStyle); toolbar.addView(toggleButton, new LayoutParams(GravityCompat.END)); toggleButton.setImageDrawable(arrowDrawable); toggleButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { toggle(); } } ); } public void syncState() { if (drawerLayout.isDrawerOpen(GravityCompat.END)) { setPosition(1f); } else { setPosition(0f); } } public void toggle() { if (drawerLayout.isDrawerOpen(GravityCompat.END)) { drawerLayout.closeDrawer(GravityCompat.END); } else { drawerLayout.openDrawer(GravityCompat.END); } } public void setPosition(float position) { if (position == 1f) { arrowDrawable.setVerticalMirror(true); toggleButton.setContentDescription(closeDrawerContentDesc); } else if (position == 0f) { arrowDrawable.setVerticalMirror(false); toggleButton.setContentDescription(openDrawerContentDesc); } arrowDrawable.setProgress(position); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { setPosition(Math.min(1f, Math.max(0, slideOffset))); } @Override public void onDrawerOpened(View drawerView) { setPosition(1f); } @Override public void onDrawerClosed(View drawerView) { setPosition(0f); } @Override public void onDrawerStateChanged(int newState) { }}该EndDrawerToggle班是一个完整的更换ActionBarDrawerToggle在这种情况下,所以你不需要任何的设置,你目前拥有的这一点。所有DrawerListener方法仍然都可以重写,但是对于基本功能来说并不需要这样做,因为EndDrawerToggle它可以自己切换抽屉状态。同样,您也无需自己处理切换点击,因此也不需要导航OnClickListener。只需实例化切换,将其添加为DrawerListener,然后进行同步即可。我建议同步onPostCreate()方法中的切换,以确保它正确同步,例如在方向更改后。private EndDrawerToggle drawerToggle;...public void initNavigationDrawer() { NavigationView navigationView = ... ... drawerLayout = (DrawerLayout)findViewById(R.id.drawer); drawerToggle = new EndDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close); drawerLayout.addDrawerListener(drawerToggle);}@Overrideprotected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState();}