web-dev-qa-db-fra.com

Comment détecter si le tiroir de navigation est ouvert?

Le titre explique tout. Tout ce que je veux faire est de savoir si le tiroir de navigation est ouvert ou non. J'ai beaucoup cherché sur le net et j'ai trouvé la méthode isDrawerOpen(int drawerGravity) mais je n'ai pas trouvé de réponse satisfaisante qui explique comment l'utiliser dans une méthode. J'apprécierais si quelqu'un me l'explique.

Merci d'avance!

85
Chinmay Dabke

En supposant que vous ayez défini un tiroirlayout en xml:

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
  //drawer is open
}
201
Neoh
 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* Host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
            ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(mTitle);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
        }
    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);

Cet auditeur utilise :)

30
ShakeJ

Utilisation:

mDrawerLayout.isDrawerOpen () méthode

Exemple:

    if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawer(Gravity.LEFT); //CLOSE Nav Drawer!
    }else{
        mDrawerLayout.openDrawer(Gravity.LEFT); //OPEN Nav Drawer!
    }
19
Jorgesys

De la manière suivante, vous pouvez trouver le tiroir ouvert ou fermé.

 public class YourActivity extends AppCompatActivity implements  DrawerLayout.DrawerListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_board);

        DrawerLayout drawer=(DrawerLayout)findViewById(R.id.container);
        drawer.setDrawerListener(this);
    }//onCreate()

    @Override
    public void onDrawerOpened(View arg0) {
      //write your code
    }

    @Override
    public void onDrawerClosed(View arg0) {
        //write your code
    }

    @Override
    public void onDrawerSlide(View arg0, float arg1) {
        //write your code
    }

    @Override
    public void onDrawerStateChanged(int arg0) {
        //write your code
    }
}//class
4
Gyan S Awasthi

la réponse de shakeJ est la bonne, et souvenez-vous que vous pouvez utiliser le onDrawerSlide pour pouvoir activer d'autres fonctions ... par exemple, je l'ai utilisée pour modifier la couleur StatusBar.

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            isOpen = false;
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            isOpen = true;
        }
        public void onDrawerSlide(View drawerView,float slideOffset){
            super.onDrawerSlide(drawerView,slideOffset);
            if(!isOpen){
                setStatusBarColor("#00102b");
            }
            if(isOpen){
                setStatusBarColor("#EFEFF0");
            }
        }
3
MontDeska

Détecter DrawerLayout ouvert, fermé, diapositive DrawerLayout.DrawerListener

DrawerLayout drawerLayout:
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    /**
     * Called when a drawer's position changes.
     *
     * @param slideOffset The new offset of this drawer within its range, from 0-1
     *                    Example when you slide drawer from left to right, slideOffset will increase from 0 - 1 (0 when drawer closed and 1 when drawer display full)
     */
    @Override
    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(@NonNull View drawerView) {
        // do something when drawer opened
    }

    @Override
    public void onDrawerClosed(@NonNull View drawerView) {
        // do something when drawer closed
    }

    /**
     * Called when the drawer motion state changes. The new state will
     * be one of {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}.
     */
    @Override
    public void onDrawerStateChanged(int newState) {

    }
});

Vérifiez le tiroir est ouvert

if(drawerLayout.isDrawerOpen(GravityCompat.START)) // or  GravityCompat.END
2
Phan Van Linh