我无法理解如何处理处理程序的导航抽屉的触摸

时间:2017-03-11 16:47:52

标签: java android navigation-drawer

我创建了一个导航抽屉,但我无法弄清楚如何在导航抽屉(处理程序)的顶部创建onClickListener。

现在我的代码是:

drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(View drawerView) {

        }

        @Override
        public void onDrawerClosed(View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    });

    navigationView = (NavigationView) findViewById(R.id.navigationView);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
    {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item)
        {
            drawerLayout.closeDrawer(navigationView);
            return false;
        }
    });



}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId())
    {
        case android.R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            break;
    }
    return true;
}

我还尝试获取包含hadler的RelativeLayout的引用,但我无法使其工作。

1 个答案:

答案 0 :(得分:0)

从NavigationView获取标题布局参考:

    View headerLayout = navigationView.getHeaderView(0);
     headerLayout.findViewById(R.id.YOUR_RELATIVE_LAYOUT).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });