How to detect that the DrawerLayout started opening?

前端 未结 7 2110
旧时难觅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: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
                    }
                }
            }
        });
    

提交回复
热议问题