android 如何关闭抽屉菜单时,点击项目菜单?

np8igboo  于 2023-06-27  发布在  Android
关注(0)|答案(3)|浏览(183)

这是点击事件。

  1. view.setOnTouchListener(new View.OnTouchListener() {
  2. @Override
  3. public boolean onTouch(View v, MotionEvent event) {
  4. v.setBackgroundResource(R.color.colorTim);
  5. FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
  6. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  7. HienThiSanPhamTheoDanhMucActivity hienThiSanPhamTheoDanhMucActivity = new HienThiSanPhamTheoDanhMucActivity();
  8. Bundle bundle = new Bundle();
  9. bundle.putInt("MALOAI",loaiSanPhams.get(groupPosition).getMALOAISP());
  10. bundle.putBoolean("KIEMTRA",false);
  11. bundle.putString("TENLOAI",loaiSanPhams.get(groupPosition).getTENLOAISP());
  12. hienThiSanPhamTheoDanhMucActivity.setArguments(bundle);
  13. fragmentTransaction.addToBackStack("TrangChuActivity");
  14. fragmentTransaction.replace(R.id.themFragment,hienThiSanPhamTheoDanhMucActivity);
  15. fragmentTransaction.commit();
  16. return false;
  17. }
  18. });

这是xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:id="@+id/drawerLayout">
  8. <FrameLayout
  9. android:id="@+id/themFragment"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent">
  12. <android.support.design.widget.CoordinatorLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:fitsSystemWindows="true"
  16. >
  17. <android.support.design.widget.AppBarLayout
  18. android:layout_height="wrap_content"
  19. android:id="@+id/appbar"
  20. android:layout_width="match_parent"
  21. android:background="@color/bgToolbar"
  22. android:fitsSystemWindows="true">
  23. <android.support.design.widget.CollapsingToolbarLayout
  24. android:id="@+id/collapsing_toolbar"
  25. android:layout_width="match_parent"
  26. android:layout_height="match_parent"
  27. app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
  28. >
  29. <android.support.v7.widget.Toolbar
  30. android:id="@+id/toolbar"
  31. android:layout_width="match_parent"
  32. android:layout_height="?attr/actionBarSize"
  33. app:theme="@style/chumenu"
  34. app:logo="@mipmap/logo"
  35. app:layout_scrollFlags="scroll|enterAlways"
  36. app:layout_collapseMode="pin"/>
  37. <LinearLayout
  38. android:id="@+id/lnSearch"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:orientation="horizontal"
  42. android:background="@drawable/bgsearch"
  43. android:layout_marginTop="55dp"
  44. android:layout_marginLeft="10dp"
  45. android:layout_marginRight="10dp"
  46. app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap"
  47. app:layout_collapseMode="parallax">
  48. <Button
  49. android:layout_width="0dp"
  50. android:layout_height="wrap_content"
  51. android:id="@+id/timkiem"
  52. android:text="@string/timkiem"
  53. android:layout_weight="1"
  54. android:textAllCaps="false"
  55. android:background="@drawable/bgsearchselected"
  56. android:textColor="@color/colorTim"
  57. android:gravity="center_vertical"
  58. android:paddingLeft="20dp"/>
  59. <View
  60. android:layout_width="1dp"
  61. android:layout_marginTop="10dp"
  62. android:layout_marginBottom="10dp"
  63. android:layout_height="match_parent"
  64. android:background="@color/bgToolbar"/>
  65. <ImageButton
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. android:layout_gravity="center"
  69. android:id="@+id/searchicon"
  70. android:padding="10dp"
  71. android:background="@drawable/bgimagesearch"
  72. android:src="@drawable/ic_search_black_24dp"/>
  73. </LinearLayout>
  74. </android.support.design.widget.CollapsingToolbarLayout>
  75. <android.support.design.widget.TabLayout
  76. android:id="@+id/tab"
  77. android:layout_width="match_parent"
  78. android:layout_height="wrap_content"
  79. app:tabTextColor="@color/colorGray"
  80. app:tabTextAppearance="@style/chuthuong"
  81. app:tabMode="scrollable">
  82. </android.support.design.widget.TabLayout>
  83. </android.support.design.widget.AppBarLayout>
  84. <FrameLayout
  85. android:id="@+id/content"
  86. android:layout_width="match_parent"
  87. android:layout_height="match_parent"
  88. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  89. >
  90. <android.support.v4.view.ViewPager
  91. android:id="@+id/viewpager"
  92. android:layout_width="match_parent"
  93. android:layout_height="match_parent">
  94. </android.support.v4.view.ViewPager>
  95. </FrameLayout>
  96. </android.support.design.widget.CoordinatorLayout>
  97. </FrameLayout>
  98. <ExpandableListView
  99. android:id="@+id/epMenu"
  100. android:layout_width="match_parent"
  101. android:layout_height="match_parent"
  102. android:layout_gravity="start"
  103. android:background="@color/colorWhite"
  104. android:choiceMode="singleChoice"
  105. android:groupIndicator="@null">
  106. </ExpandableListView>
  107. </android.support.v4.widget.DrawerLayout>

结果将显示在片段中,但抽屉式菜单仍显示。我该怎么做才能关闭它?任何人给我的建议,如何解决这个问题,任何帮助非常感谢。谢谢你。

o2rvlv0m

o2rvlv0m1#

onTouchListener中添加此选项将在您每次触摸任何项目时关闭抽屉:

  1. drawer.closeDrawer(GravityCompat.START);
mccptt67

mccptt672#

将以下代码添加到www.example.com的onCreate()MainActivity.java:

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

然后添加下面的代码:

  1. private NavigationView.OnNavigationItemSelectedListener mOnNavigationDrawerItemSelectedListener
  2. = new NavigationView.OnNavigationItemSelectedListener() {
  3. @Override
  4. public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
  5. Fragment fragment;
  6. int id = menuItem.getItemId();
  7. switch (id) {
  8. case R.id.nav_reload_downloaded_tasks:
  9. toolbarTitle.setText(R.string.menu_reload_downloaded_tasks);
  10. fragment = new ReloadDownloadedTasksFragment();
  11. loadFragment(fragment);
  12. break;
  13. case R.id.nav_download_base_data:
  14. toolbarTitle.setText(R.string.menu_download_base_data);
  15. fragment = new ReloadDownloadedTasksFragment();
  16. loadFragment(fragment);
  17. break;
  18. }
  19. DrawerLayout drawer = findViewById(R.id.drawer_layout);
  20. drawer.closeDrawer(GravityCompat.START);
  21. return true;
  22. }
  23. };

还有这个

  1. private void loadFragment(Fragment fragment) {
  2. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  3. transaction.replace(R.id.nav_host_fragment, fragment);
  4. transaction.addToBackStack(null);
  5. transaction.commit();
  6. }
展开查看全部
szqfcxe2

szqfcxe23#

  1. view.setOnTouchListener(new View.OnTouchListener() {
  2. @Override
  3. public boolean onTouch(View v, MotionEvent event) {
  4. v.setBackgroundResource(R.color.colorTim);
  5. FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
  6. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  7. HienThiSanPhamTheoDanhMucActivity hienThiSanPhamTheoDanhMucActivity = new HienThiSanPhamTheoDanhMucActivity();
  8. Bundle bundle = new Bundle();
  9. bundle.putInt("MALOAI",loaiSanPhams.get(groupPosition).getMALOAISP());
  10. bundle.putBoolean("KIEMTRA",false);
  11. bundle.putString("TENLOAI",loaiSanPhams.get(groupPosition).getTENLOAISP());
  12. hienThiSanPhamTheoDanhMucActivity.setArguments(bundle);
  13. fragmentTransaction.addToBackStack("TrangChuActivity");
  14. fragmentTransaction.replace(R.id.themFragment,hienThiSanPhamTheoDanhMucActivity);
  15. fragmentTransaction.commit();
  16. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  17. drawer.closeDrawer(GravityCompat.START);
  18. return false;
  19. }
  20. });
展开查看全部

相关问题