英文:
Navigation Drawer is not handling click events
问题
问题1:我的导航抽屉不能处理点击事件。
问题2:它显示的是向后的箭头,而不是汉堡图标。
我已经尝试过搜索,但找不到答案。我还尝试了添加.bringToFront()
方法,但仍然不起作用。
MainActivity.java:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView navUserName = findViewById(R.id.textViewNav_username);
TextView navUserEmail = findViewById(R.id.textViewNav_useremail);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference volunteerRef = dbRef.child("Volunteer").child(currentUser.getUid());
if (currentUser == null) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle actionBarDrawerToggle= new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.bringToFront();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_option_home);
mAppBarConfiguration = new AppBarConfiguration.Builder(
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)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Log.d("main1122", "inside listner");
switch (item.getItemId()){
case R.id.nav_option_home:
Log.d("main1122", "Clicked item" + item.getItemId());
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new HomeFragment()).commit();
break;
case R.id.nav_option_profile:
Log.d("main1122", "Clicked item" + item.getItemId());
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new profile()).commit();
break;
case R.id.nav_option_contact:
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new contact()).commit();
break;
case R.id.nav_option_about:
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new AboutFragment()).commit();
break;
case R.id.nav_option_share:
Toast.makeText(this, "Share clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_option_logout:
// TODO: Logout action
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
activity_main.xml(包含R.id.nav_view
):-
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_nav__drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_nav__drawer"
app:menu="@menu/activity_nav__drawer_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
mobile_navigation.xml(包含我想在点击菜单项后启动的片段):-
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.helpinghandsorg.helpinghands.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<!-- 其他片段 -->
</navigation>
activity_nav_drawer_drawer.xml(包含菜单项的ID):-
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navi_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_option_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/menu_home" />
<item
android:id="@+id/nav_option_profile"
android:icon="@drawable/ic_account_circle_black_24dp"
android:title="@string/menu_profile" />
<item
android:id="@+id/nav_option_contact"
android:icon="@drawable/ic_email_black_24dp"
android:title="@string/menu_contact" />
<item
android:id="@+id/nav_option_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="@string/menu_about" />
<item
android:id="@+id/nav_option_share"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_share" />
<item
android:id="@+id/nav_option_logout"
android:icon="@drawable/ic_exit_to_app_black_24dp"
android:title="@string/menu_logout" />
</group>
</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:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView navUserName = findViewById(R.id.textViewNav_username);
TextView navUserEmail = findViewById(R.id.textViewNav_useremail);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference volunteerRef = dbRef.child("Volunteer").child(currentUser.getUid());
//If current user is null go to login activity
if (currentUser == null) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
//Creates hamburger animated icon
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle actionBarDrawerToggle= new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
//Sets click listner to navigation item 1/2
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.bringToFront();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_option_home);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
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)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
//Sets click listner to navigation item 2/2
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Log.d("main1122", "inside listner");
switch (item.getItemId()){
case R.id.nav_option_home:
Log.d("main1122", "Clicked item" + item.getItemId());
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new HomeFragment()).commit();
break;
case R.id.nav_option_profile:
Log.d("main1122", "Clicked item" + item.getItemId());
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new profile()).commit();
break;
case R.id.nav_option_contact:
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new contact()).commit();
break;
case R.id.nav_option_about:
getSupportFragmentManager().beginTransaction().replace(R.id.content_nav_drawer,
new AboutFragment()).commit();
break;
case R.id.nav_option_share:
//TODO: Share app action
Toast.makeText(this,"Share clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_option_logout:
//TODO: Logout action
break;
}
drawer.closeDrawer(GravityCompat.START);
//Return false will make menu item unselected(not highlighted)
return true;
}
activity_main.xml (Contains R.id.nav_view) :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_nav__drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_nav__drawer"
app:menu="@menu/activity_nav__drawer_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
mobile_navigation.xml (Contains fragments which I want to launch after clicking menu items) :-
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.helpinghandsorg.helpinghands.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home"/>
<fragment
android:id="@+id/nav_about"
android:name="com.helpinghandsorg.helpinghands.AboutFragment"
android:label="@string/menu_about"
tools:layout="@layout/fragment_about">
<action
android:id="@+id/action_nav_about_to_nav_home"
app:destination="@id/nav_home" />
</fragment>
<fragment
android:id="@+id/nav_contact"
android:name="com.helpinghandsorg.helpinghands.ui.contact.contact"
android:label="@string/menu_contact"
tools:layout="@layout/contact_fragment">
<action
android:id="@+id/action_nav_contact_to_nav_home"
app:destination="@id/nav_home" />
</fragment>
<fragment
android:id="@+id/nav_profile"
android:name="com.helpinghandsorg.helpinghands.ui.profile.profile"
android:label="@string/menu_profile"
tools:layout="@layout/profile_fragment">
<action
android:id="@+id/action_nav_profile_to_nav_home"
app:destination="@id/nav_home" />
</fragment>
</navigation>
activity_nav_drawer_drawer.xml (Contains Menu Item IDs):-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navi_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_option_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/menu_home" />
<item
android:id="@+id/nav_option_profile"
android:icon="@drawable/ic_account_circle_black_24dp"
android:title="@string/menu_profile" />
<item
android:id="@+id/nav_option_contact"
android:icon="@drawable/ic_email_black_24dp"
android:title="@string/menu_contact" />
<item
android:id="@+id/nav_option_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="@string/menu_about" />
<item
android:id="@+id/nav_option_share"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_share" />
<item
android:id="@+id/nav_option_logout"
android:icon="@drawable/ic_exit_to_app_black_24dp"
android:title="@string/menu_logout" />
</group>
</menu>
答案1
得分: 0
问题- 而不是显示汉堡图标,它显示为向后的箭头。
解决方案- 实际上,这是因为我没有在AppbarConfiguration.Builder中提供主页片段的ID。
之前-
mAppBarConfiguration = new AppBarConfiguration.Builder(
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)
.setDrawerLayout(drawer)
.build();
之后-
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home,
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)
.setDrawerLayout(drawer)
.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-
mAppBarConfiguration = new AppBarConfiguration.Builder(
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)
.setDrawerLayout(drawer)
.build();
After-
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home,
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)
.setDrawerLayout(drawer)
.build();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论