Android NavigationView何时在屏幕侦听器上可见

时间:2018-09-03 11:45:07

标签: android

是否激活了NavigationView的侦听器(在屏幕上可见)? 我尝试了setonsystemuivisibility,但没有用。

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

我需要在屏幕上或屏幕消失时调用监听器。 谢谢。

2 个答案:

答案 0 :(得分:2)

添加一个DrawerListener

    mDrawerLayout = findViewById(R.id.drawer_layout);
    mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() 
    {
        // other overridden methods not shown 

        @Override
        public void onDrawerOpened(View drawerView) 
        {
            // drawer opened
            // todo: announce for accessibility 
        } 
    });

答案 1 :(得分:1)

希望这对您有帮助

DrawerLayout mDrawerLayout;
public static ActionBarDrawerToggle mDrawerToggle;


mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);



mDrawerToggle = new ActionBarDrawerToggle(FragmentHomeActivity.this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name){


                public void onDrawerClosed(View view) {
                    //calling onDrawerClosed, when View gone invisible

                }

                public void onDrawerOpened(View drawerView) {
                    // calling onDrawerOpened, when View visible
                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);

如果您使用的是Toolbar,则

toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);