How to close drawer menu when click on item menu?

半腔热情 提交于 2019-12-01 09:42:59

Adding this inside your onTouchListener will close the drawer every time you touch any of the items:

drawer.closeDrawer(GravityCompat.START);
santosh kumar
view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {


            v.setBackgroundResource(R.color.colorTim);

            FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            HienThiSanPhamTheoDanhMucActivity hienThiSanPhamTheoDanhMucActivity = new HienThiSanPhamTheoDanhMucActivity();

            Bundle bundle = new Bundle();

            bundle.putInt("MALOAI",loaiSanPhams.get(groupPosition).getMALOAISP());
            bundle.putBoolean("KIEMTRA",false);
            bundle.putString("TENLOAI",loaiSanPhams.get(groupPosition).getTENLOAISP());

            hienThiSanPhamTheoDanhMucActivity.setArguments(bundle);
            fragmentTransaction.addToBackStack("TrangChuActivity");
            fragmentTransaction.replace(R.id.themFragment,hienThiSanPhamTheoDanhMucActivity);
            fragmentTransaction.commit();

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return false;
        }
    });

Add below code to onCreate() of MainActivity.java:

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

And then add below code:

    private NavigationView.OnNavigationItemSelectedListener mOnNavigationDrawerItemSelectedListener
        = new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Fragment fragment;
        int id = menuItem.getItemId();

        switch (id) {
            case R.id.nav_reload_downloaded_tasks:
                toolbarTitle.setText(R.string.menu_reload_downloaded_tasks);
                fragment = new ReloadDownloadedTasksFragment();
                loadFragment(fragment);
                break;

            case R.id.nav_download_base_data:
                toolbarTitle.setText(R.string.menu_download_base_data);
                fragment = new ReloadDownloadedTasksFragment();
                loadFragment(fragment);
                break;
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
};

And this:

  private void loadFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.nav_host_fragment, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

enter code here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!