问题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 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 (包含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 (包含我要在单击菜单项后启动的片段):-
<?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 (包含菜单项ID):-
<?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>
答案 0 :(得分:0)
第二个问题的答案。 问题-不是显示汉堡图标,而是显示向后箭头。
解决方案-实际上是因为我没有在AppbarConfiguration.Builder上提供home片段的ID
之前- mAppBarConfiguration =新的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(抽屉) .build();
之后- mAppBarConfiguration =新的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(抽屉) .build();