How to detect that the DrawerLayout started opening?

前端 未结 7 2067
旧时难觅i
旧时难觅i 2020-12-01 04:52

So I have tabs that I want to hide when the Navigation Drawer starts opening. The code I have hides them when it finished opening, but it\'s not what I want

相关标签:
7条回答
  • 2020-12-01 05:23

    Currently accepted answer by Pavel Dudka is already deprecated. Please use mDrawerLayout.addDrawerListener() method instead to set a listener.

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //Called when a drawer's position changes.
            }
    
            @Override
            public void onDrawerOpened(View drawerView) {
                //Called when a drawer has settled in a completely open state.
                //The drawer is interactive at this point.
                // If you have 2 drawers (left and right) you can distinguish 
                // them by using id of the drawerView. int id = drawerView.getId(); 
                // id will be your layout's id: for example R.id.left_drawer            
            }
    
            @Override
            public void onDrawerClosed(View drawerView) {
                // Called when a drawer has settled in a completely closed state.
            }
    
            @Override
            public void onDrawerStateChanged(int newState) {
                // Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
            }
        });
    

    Works perfectly. Cheers!

    0 讨论(0)
  • 2020-12-01 05:34

    Up-to-date solution:

    As others have suggested, the current answer is outdated and it's advised to use mDrawerLayout.addDrawerListener(). A working solution would then be:

    mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {
                if (newState == DrawerLayout.STATE_SETTLING && !mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    // Drawer started opening
                }
            }
        });
    

    Naturally, replace GravityCompat.START with whatever identifies your drawer (layout ID or its gravity ~ location).

    Also, if you want to detect when the drawer starts closing, you can simply do:

    mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {
                if (newState == DrawerLayout.STATE_SETTLING) {
                    if (!mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                        // Drawer started opening
                    } else {
                        // Drawer started closing
                    }
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-01 05:35

    try to override a method of DrawerLayout.DrawerListener

    @Override
    public void onDrawerStateChanged(int newState) {
        if( newState == DrawerLayout.STATE_DRAGGING && isDrawerOpen() == false ) {
            // this where Drawer start opening
        }
    }
    
    0 讨论(0)
  • 2020-12-01 05:36

    For Kotlin

    var toggle = object : ActionBarDrawerToggle(this,
                    drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
    
                override fun onDrawerOpened(drawerView: View) {
                    super.onDrawerOpened(drawerView)
                }
            }
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
    
    0 讨论(0)
  • 2020-12-01 05:38

    There are 2 possible ways to do that:

    1) Use onDrawerSlide(View drawerView, float slideOffset) callback

    slideOffset changes from 0 to 1. 1 means it is completely open, 0 - closed.

    Once offset changes from 0 to !0 - it means it started opening process. Something like:

    mDrawerToggle = new ActionBarDrawerToggle(
            this,                 
            mDrawerLayout,        
            R.drawable.ic_drawer,  
            R.string.drawer_open,  
            R.string.drawer_close  
    ) {
    
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            if (slideOffset == 0
                    && getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD) {
                // drawer closed
                getActionBar()
                        .setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                invalidateOptionsMenu();
            } else if (slideOffset != 0
                    && getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
                // started opening
                getActionBar()
                        .setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
                invalidateOptionsMenu();
            }
            super.onDrawerSlide(drawerView, slideOffset);
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    

    2) Use onDrawerStateChanged(int newState) callback

    You need to listen to STATE_SETTLING states - this state is reported whenever drawer starts moving (either opens or closes). So once you see this state - check whether drawer is opened now and act accordingly:

    mDrawerToggle = new ActionBarDrawerToggle(
            this,                 
            mDrawerLayout,        
            R.drawable.ic_drawer,  
            R.string.drawer_open,  
            R.string.drawer_close  
    ) {
        @Override
        public void onDrawerStateChanged(int newState) {
            if (newState == DrawerLayout.STATE_SETTLING) {
                if (!isDrawerOpen()) {
                    // starts opening
                    getActionBar()
                            .setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
                } else {
                    // closing drawer
                    getActionBar()
                            .setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                }
                invalidateOptionsMenu();
            }
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    
    0 讨论(0)
  • 2020-12-01 05:40
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
          @Override
          public void onDrawerOpened(View drawerView) {
               super.onDrawerOpened(drawerView);
               app.sendScreenView("Menu");
          }
    };
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    

    It's the best way.

    0 讨论(0)
提交回复
热议问题