导航抽屉未处理点击事件。

huangapple go评论105阅读模式
英文:

Navigation Drawer is not handling click events

问题

问题1:我的导航抽屉不能处理点击事件。

问题2:它显示的是向后的箭头,而不是汉堡图标。

我已经尝试过搜索,但找不到答案。我还尝试了添加.bringToFront()方法,但仍然不起作用。

MainActivity.java:-

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. Toolbar toolbar = findViewById(R.id.toolbar);
  5. setSupportActionBar(toolbar);
  6. TextView navUserName = findViewById(R.id.textViewNav_username);
  7. TextView navUserEmail = findViewById(R.id.textViewNav_useremail);
  8. mAuth = FirebaseAuth.getInstance();
  9. currentUser = mAuth.getCurrentUser();
  10. DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
  11. DatabaseReference volunteerRef = dbRef.child("Volunteer").child(currentUser.getUid());
  12. if (currentUser == null) {
  13. Intent intent = new Intent(this, LoginActivity.class);
  14. startActivity(intent);
  15. finish();
  16. }
  17. drawer = findViewById(R.id.drawer_layout);
  18. ActionBarDrawerToggle actionBarDrawerToggle= new ActionBarDrawerToggle(this, drawer, toolbar,
  19. R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  20. drawer.addDrawerListener(actionBarDrawerToggle);
  21. actionBarDrawerToggle.syncState();
  22. NavigationView navigationView = findViewById(R.id.nav_view);
  23. navigationView.bringToFront();
  24. navigationView.setNavigationItemSelectedListener(this);
  25. navigationView.setCheckedItem(R.id.nav_option_home);
  26. mAppBarConfiguration = new AppBarConfiguration.Builder(
  27. R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share, R.id.nav_option_logout)
  28. .setDrawerLayout(drawer)
  29. .build();
  30. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  31. NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
  32. NavigationUI.setupWithNavController(navigationView, navController);
  33. }
  34. @Override
  35. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  36. Log.d("main1122", "inside listner");
  37. switch (item.getItemId()){
  38. case R.id.nav_option_home:
  39. Log.d("main1122", "Clicked item" + item.getItemId());
  40. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  41. new HomeFragment()).commit();
  42. break;
  43. case R.id.nav_option_profile:
  44. Log.d("main1122", "Clicked item" + item.getItemId());
  45. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  46. new profile()).commit();
  47. break;
  48. case R.id.nav_option_contact:
  49. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  50. new contact()).commit();
  51. break;
  52. case R.id.nav_option_about:
  53. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  54. new AboutFragment()).commit();
  55. break;
  56. case R.id.nav_option_share:
  57. Toast.makeText(this, "Share clicked", Toast.LENGTH_SHORT).show();
  58. break;
  59. case R.id.nav_option_logout:
  60. // TODO: Logout action
  61. break;
  62. }
  63. drawer.closeDrawer(GravityCompat.START);
  64. return true;
  65. }

activity_main.xml(包含R.id.nav_view):-

  1. <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/drawer_layout"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:fitsSystemWindows="true"
  8. tools:openDrawer="start">
  9. <include
  10. layout="@layout/app_bar_nav__drawer"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent" />
  13. <com.google.android.material.navigation.NavigationView
  14. android:id="@+id/nav_view"
  15. android:layout_width="wrap_content"
  16. android:layout_height="match_parent"
  17. android:layout_gravity="start"
  18. android:fitsSystemWindows="true"
  19. app:headerLayout="@layout/nav_header_nav__drawer"
  20. app:menu="@menu/activity_nav__drawer_drawer" />
  21. </androidx.drawerlayout.widget.DrawerLayout>

mobile_navigation.xml(包含我想在点击菜单项后启动的片段):-

  1. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/mobile_navigation"
  5. app:startDestination="@id/nav_home">
  6. <fragment
  7. android:id="@+id/nav_home"
  8. android:name="com.helpinghandsorg.helpinghands.ui.home.HomeFragment"
  9. android:label="@string/menu_home"
  10. tools:layout="@layout/fragment_home" />
  11. <!-- 其他片段 -->
  12. </navigation>

activity_nav_drawer_drawer.xml(包含菜单项的ID):-

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. tools:showIn="navi_view">
  4. <group android:checkableBehavior="single">
  5. <item
  6. android:id="@+id/nav_option_home"
  7. android:icon="@drawable/ic_home_black_24dp"
  8. android:title="@string/menu_home" />
  9. <item
  10. android:id="@+id/nav_option_profile"
  11. android:icon="@drawable/ic_account_circle_black_24dp"
  12. android:title="@string/menu_profile" />
  13. <item
  14. android:id="@+id/nav_option_contact"
  15. android:icon="@drawable/ic_email_black_24dp"
  16. android:title="@string/menu_contact" />
  17. <item
  18. android:id="@+id/nav_option_about"
  19. android:icon="@drawable/ic_info_black_24dp"
  20. android:title="@string/menu_about" />
  21. <item
  22. android:id="@+id/nav_option_share"
  23. android:icon="@drawable/ic_menu_share"
  24. android:title="@string/menu_share" />
  25. <item
  26. android:id="@+id/nav_option_logout"
  27. android:icon="@drawable/ic_exit_to_app_black_24dp"
  28. android:title="@string/menu_logout" />
  29. </group>
  30. </menu>
英文:

Problem 1- My navigation drawer is not handling click events.

Problem 2- Instead of showing hamburger icon it is showing backward arrow.

I tried searching already but couldn't find answer. I also tried adding .bringToFront() method but still not working.

MainActivity.java:-

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. Toolbar toolbar = findViewById(R.id.toolbar);
  5. setSupportActionBar(toolbar);
  6. TextView navUserName = findViewById(R.id.textViewNav_username);
  7. TextView navUserEmail = findViewById(R.id.textViewNav_useremail);
  8. mAuth = FirebaseAuth.getInstance();
  9. currentUser = mAuth.getCurrentUser();
  10. DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
  11. DatabaseReference volunteerRef = dbRef.child(&quot;Volunteer&quot;).child(currentUser.getUid());
  12. //If current user is null go to login activity
  13. if (currentUser == null) {
  14. Intent intent = new Intent(this, LoginActivity.class);
  15. startActivity(intent);
  16. finish();
  17. }
  18. //Creates hamburger animated icon
  19. drawer = findViewById(R.id.drawer_layout);
  20. ActionBarDrawerToggle actionBarDrawerToggle= new ActionBarDrawerToggle(this, drawer, toolbar,
  21. R.string.navigation_drawer_open,R.string.navigation_drawer_close);
  22. drawer.addDrawerListener(actionBarDrawerToggle);
  23. actionBarDrawerToggle.syncState();
  24. //Sets click listner to navigation item 1/2
  25. NavigationView navigationView = findViewById(R.id.nav_view);
  26. navigationView.bringToFront();
  27. navigationView.setNavigationItemSelectedListener(this);
  28. navigationView.setCheckedItem(R.id.nav_option_home);
  29. // Passing each menu ID as a set of Ids because each
  30. // menu should be considered as top level destinations.
  31. mAppBarConfiguration = new AppBarConfiguration.Builder(
  32. R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
  33. .setDrawerLayout(drawer)
  34. .build();
  35. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  36. NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
  37. NavigationUI.setupWithNavController(navigationView, navController);
  38. }
  39. //Sets click listner to navigation item 2/2
  40. @Override
  41. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  42. Log.d(&quot;main1122&quot;, &quot;inside listner&quot;);
  43. switch (item.getItemId()){
  44. case R.id.nav_option_home:
  45. Log.d(&quot;main1122&quot;, &quot;Clicked item&quot; + item.getItemId());
  46. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  47. new HomeFragment()).commit();
  48. break;
  49. case R.id.nav_option_profile:
  50. Log.d(&quot;main1122&quot;, &quot;Clicked item&quot; + item.getItemId());
  51. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  52. new profile()).commit();
  53. break;
  54. case R.id.nav_option_contact:
  55. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  56. new contact()).commit();
  57. break;
  58. case R.id.nav_option_about:
  59. getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
  60. new AboutFragment()).commit();
  61. break;
  62. case R.id.nav_option_share:
  63. //TODO: Share app action
  64. Toast.makeText(this,&quot;Share clicked&quot;,Toast.LENGTH_SHORT).show();
  65. break;
  66. case R.id.nav_option_logout:
  67. //TODO: Logout action
  68. break;
  69. }
  70. drawer.closeDrawer(GravityCompat.START);
  71. //Return false will make menu item unselected(not highlighted)
  72. return true;
  73. }

activity_main.xml (Contains R.id.nav_view) :-

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:id=&quot;@+id/drawer_layout&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;
  8. android:fitsSystemWindows=&quot;true&quot;
  9. tools:openDrawer=&quot;start&quot;&gt;
  10. &lt;include
  11. layout=&quot;@layout/app_bar_nav__drawer&quot;
  12. android:layout_width=&quot;match_parent&quot;
  13. android:layout_height=&quot;match_parent&quot; /&gt;
  14. &lt;com.google.android.material.navigation.NavigationView
  15. android:id=&quot;@+id/nav_view&quot;
  16. android:layout_width=&quot;wrap_content&quot;
  17. android:layout_height=&quot;match_parent&quot;
  18. android:layout_gravity=&quot;start&quot;
  19. android:fitsSystemWindows=&quot;true&quot;
  20. app:headerLayout=&quot;@layout/nav_header_nav__drawer&quot;
  21. app:menu=&quot;@menu/activity_nav__drawer_drawer&quot; /&gt;
  22. &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

mobile_navigation.xml (Contains fragments which I want to launch after clicking menu items) :-

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:id=&quot;@+id/mobile_navigation&quot;
  6. app:startDestination=&quot;@+id/nav_home&quot;&gt;
  7. &lt;fragment
  8. android:id=&quot;@+id/nav_home&quot;
  9. android:name=&quot;com.helpinghandsorg.helpinghands.ui.home.HomeFragment&quot;
  10. android:label=&quot;@string/menu_home&quot;
  11. tools:layout=&quot;@layout/fragment_home&quot;/&gt;
  12. &lt;fragment
  13. android:id=&quot;@+id/nav_about&quot;
  14. android:name=&quot;com.helpinghandsorg.helpinghands.AboutFragment&quot;
  15. android:label=&quot;@string/menu_about&quot;
  16. tools:layout=&quot;@layout/fragment_about&quot;&gt;
  17. &lt;action
  18. android:id=&quot;@+id/action_nav_about_to_nav_home&quot;
  19. app:destination=&quot;@id/nav_home&quot; /&gt;
  20. &lt;/fragment&gt;
  21. &lt;fragment
  22. android:id=&quot;@+id/nav_contact&quot;
  23. android:name=&quot;com.helpinghandsorg.helpinghands.ui.contact.contact&quot;
  24. android:label=&quot;@string/menu_contact&quot;
  25. tools:layout=&quot;@layout/contact_fragment&quot;&gt;
  26. &lt;action
  27. android:id=&quot;@+id/action_nav_contact_to_nav_home&quot;
  28. app:destination=&quot;@id/nav_home&quot; /&gt;
  29. &lt;/fragment&gt;
  30. &lt;fragment
  31. android:id=&quot;@+id/nav_profile&quot;
  32. android:name=&quot;com.helpinghandsorg.helpinghands.ui.profile.profile&quot;
  33. android:label=&quot;@string/menu_profile&quot;
  34. tools:layout=&quot;@layout/profile_fragment&quot;&gt;
  35. &lt;action
  36. android:id=&quot;@+id/action_nav_profile_to_nav_home&quot;
  37. app:destination=&quot;@id/nav_home&quot; /&gt;
  38. &lt;/fragment&gt;
  39. &lt;/navigation&gt;

activity_nav_drawer_drawer.xml (Contains Menu Item IDs):-

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. tools:showIn=&quot;navi_view&quot;&gt;
  5. &lt;group android:checkableBehavior=&quot;single&quot;&gt;
  6. &lt;item
  7. android:id=&quot;@+id/nav_option_home&quot;
  8. android:icon=&quot;@drawable/ic_home_black_24dp&quot;
  9. android:title=&quot;@string/menu_home&quot; /&gt;
  10. &lt;item
  11. android:id=&quot;@+id/nav_option_profile&quot;
  12. android:icon=&quot;@drawable/ic_account_circle_black_24dp&quot;
  13. android:title=&quot;@string/menu_profile&quot; /&gt;
  14. &lt;item
  15. android:id=&quot;@+id/nav_option_contact&quot;
  16. android:icon=&quot;@drawable/ic_email_black_24dp&quot;
  17. android:title=&quot;@string/menu_contact&quot; /&gt;
  18. &lt;item
  19. android:id=&quot;@+id/nav_option_about&quot;
  20. android:icon=&quot;@drawable/ic_info_black_24dp&quot;
  21. android:title=&quot;@string/menu_about&quot; /&gt;
  22. &lt;item
  23. android:id=&quot;@+id/nav_option_share&quot;
  24. android:icon=&quot;@drawable/ic_menu_share&quot;
  25. android:title=&quot;@string/menu_share&quot; /&gt;
  26. &lt;item
  27. android:id=&quot;@+id/nav_option_logout&quot;
  28. android:icon=&quot;@drawable/ic_exit_to_app_black_24dp&quot;
  29. android:title=&quot;@string/menu_logout&quot; /&gt;
  30. &lt;/group&gt;
  31. &lt;/menu&gt;

答案1

得分: 0

问题- 而不是显示汉堡图标,它显示为向后的箭头。

解决方案- 实际上,这是因为我没有在AppbarConfiguration.Builder中提供主页片段的ID。

之前-

  1. mAppBarConfiguration = new AppBarConfiguration.Builder(
  2. R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
  3. .setDrawerLayout(drawer)
  4. .build();

之后-

  1. mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home,
  2. R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
  3. .setDrawerLayout(drawer)
  4. .build();
英文:

Answer for my 2nd question.
PROBLEM- Instead of showing hamburger icon it is showing backward arrow.

Solution- Actually it was because I didn't provide ID of home fragment on my AppbarConfiguration.Builder

Before-

  1. mAppBarConfiguration = new AppBarConfiguration.Builder(
  2. R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
  3. .setDrawerLayout(drawer)
  4. .build();

After-

  1. mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home,
  2. R.id.nav_option_profile, R.id.nav_option_contact, R.id.nav_option_about, R.id.nav_option_share,R.id.nav_option_logout)
  3. .setDrawerLayout(drawer)
  4. .build();

huangapple
  • 本文由 发表于 2020年4月10日 15:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61135891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定