How to close drawer menu when click on item menu?

2020-03-25 16:28发布

ScreenShot

This is click event.

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();
        return false;
    }
});

This is xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout">

    <FrameLayout
        android:id="@+id/themFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            >


            <android.support.design.widget.AppBarLayout
                android:layout_height="wrap_content"
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:background="@color/bgToolbar"
                android:fitsSystemWindows="true">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                    >
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:theme="@style/chumenu"
                        app:logo="@mipmap/logo"
                        app:layout_scrollFlags="scroll|enterAlways"
                        app:layout_collapseMode="pin"/>

                    <LinearLayout
                        android:id="@+id/lnSearch"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:background="@drawable/bgsearch"
                        android:layout_marginTop="55dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap"
                        app:layout_collapseMode="parallax">

                        <Button
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:id="@+id/timkiem"
                            android:text="@string/timkiem"
                            android:layout_weight="1"
                            android:textAllCaps="false"
                            android:background="@drawable/bgsearchselected"
                            android:textColor="@color/colorTim"
                            android:gravity="center_vertical"
                            android:paddingLeft="20dp"/>
                        <View
                            android:layout_width="1dp"
                            android:layout_marginTop="10dp"
                            android:layout_marginBottom="10dp"
                            android:layout_height="match_parent"
                            android:background="@color/bgToolbar"/>
                        <ImageButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:id="@+id/searchicon"
                            android:padding="10dp"
                            android:background="@drawable/bgimagesearch"
                            android:src="@drawable/ic_search_black_24dp"/>

                    </LinearLayout>
                </android.support.design.widget.CollapsingToolbarLayout>

                <android.support.design.widget.TabLayout
                    android:id="@+id/tab"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabTextColor="@color/colorGray"
                    app:tabTextAppearance="@style/chuthuong"
                    app:tabMode="scrollable">

                </android.support.design.widget.TabLayout>

            </android.support.design.widget.AppBarLayout>
            <FrameLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                >
                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </android.support.v4.view.ViewPager>

            </FrameLayout>
        </android.support.design.widget.CoordinatorLayout>
    </FrameLayout>

    <ExpandableListView
        android:id="@+id/epMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorWhite"
        android:choiceMode="singleChoice"
        android:groupIndicator="@null">

    </ExpandableListView>

</android.support.v4.widget.DrawerLayout>

Results will be displayed in a Fragment but drawer menu still display. What might I do to close It? any one gives me suggestions that how to solve this, Any help much appreciated. Thank you.

3条回答
\"骚年 ilove
2楼-- · 2020-03-25 16:56
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;
        }
    });
查看更多
再贱就再见
3楼-- · 2020-03-25 16:56

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

查看更多
孤傲高冷的网名
4楼-- · 2020-03-25 17:07

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

drawer.closeDrawer(GravityCompat.START);
查看更多
登录 后发表回答