抽屉上的 Android 已关闭侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26082467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:58:59  来源:igfitidea点击:

Android on Drawer Closed Listener

androidlistenernavigation-drawer

提问by rendyhihi

I have an application using navigation drawer that provides list of locations. In the drawer, there are several options (like choosing country, city, etc) that user can setup before showing the corresponding list in the main activity.

我有一个使用导航抽屉的应用程序,它提供了位置列表。在抽屉中,用户可以在主活动中显示相应列表之前设置几个选项(如选择国家、城市等)。

Is there any possibility to refresh the list when user close the drawer, or maybe there is another way to solve this? I've tried to search for tutorials but found nothing about this drawer closed listener. Any suggestions would be helpful, thanks!

当用户关闭抽屉时是否有可能刷新列表,或者可能有另一种方法来解决这个问题?我试图搜索教程,但没有发现有关此抽屉关闭侦听器的任何信息。任何建议都会有所帮助,谢谢!

回答by reVerse

When you setup the ActionBarDrawerToggleyou can "implement"the onDrawerClosedand onDrawerOpenedcallbacks. See the following example from the Docs:

当你安装了ActionBarDrawerToggle可以“实现”onDrawerClosedonDrawerOpened回调。请参阅文档中的以下示例:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            // Do whatever you want here
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // Do whatever you want here
        }
    };
// Set the drawer toggle as the DrawerListener
mDrawerLayout.addDrawerListener(mDrawerToggle);

Edit: Now the setDrawerListener is deprecated, use addDrawerListener instead.

编辑:现在 setDrawerListener 已弃用,请改用 addDrawerListener。

回答by Opster Elasticsearch Support

reVerse answer is right in case you are using ActionBar as well. in case you just use the DrawerLayout directly, you can add a DrawerListener to it:

如果您也使用 ActionBar,反向答案是正确的。如果您只是直接使用 DrawerLayout,则可以向其添加 DrawerListener:

View drawerView = findViewById(R.id.drawer_layout);
if (drawerView != null && drawerView instanceof DrawerLayout) {
    mDrawer = (DrawerLayout)drawerView;
    mDrawer.setDrawerListener(new DrawerListener() {
            @Override
            public void onDrawerSlide(View view, float v) {

            }

            @Override
            public void onDrawerOpened(View view) {

            }

            @Override
            public void onDrawerClosed(View view) {
                // your refresh code can be called from here
            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });
}


As per kit's comment, addDrawerListener()should be used now that setDrawerListener()has been deprecated.

根据套件的评论addDrawerListener()现在应该使用setDrawerListener()已弃用。

回答by Suraj Vaishnav

setDrawerListeneris deprecated use addDrawerListenerinstead.

setDrawerListener已弃用addDrawerListener

drawerLayout.addDrawerListener(new DrawerListener() {
                @Override
                public void onDrawerSlide(View view, float v) {

                }

                @Override
                public void onDrawerOpened(View view) {

                }

                @Override
                public void onDrawerClosed(View view) {

                }

                @Override
                public void onDrawerStateChanged(int i) {

                }
            });