【发布时间】:2022-08-13 21:35:03
【问题描述】:
我正在使用最新 Android Studio(2021.2.1 补丁 2)中的“导航抽屉活动”模板作为开始。
通过单击 imageViews,我能够以编程方式在片段之间导航。
但是在这些成功的导航之后,单击菜单中的“主页”项不起作用。 (Gif)
https://developer.android.com/guide/navigation/navigation-navigate https://developer.android.com/guide/navigation/navigation-programmatic
HomeFragment.java:(已编辑)
final ImageView imageView = binding.imageView2;
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Fragment navHostFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);
NavController navController = NavHostFragment.findNavController(navHostFragment);
navController.navigate(R.id.nav_slideshow);
}
});
SlideshowFragment.java:(已编辑)
final ImageView imageView = binding.imageView3;
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Fragment navHostFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);
NavController navController = NavHostFragment.findNavController(navHostFragment);
navController.navigate(R.id.nav_home);
}
});
MainActivity.java:(未编辑)
package com.example.try3;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.example.try3.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
binding.appBarMain.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, \"Replace with your own action\", Snackbar.LENGTH_LONG)
.setAction(\"Action\", null).show();
}
});
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// 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_home, R.id.nav_gallery, R.id.nav_slideshow)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
fragment_home.xml:(未编辑)
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
tools:context=\".ui.home.HomeFragment\">
<ImageView
android:id=\"@+id/imageView2\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:src=\"@drawable/ic_launcher_background\"
app:layout_constraintStart_toStartOf=\"parent\"
app:layout_constraintTop_toTopOf=\"parent\" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_slideshow.xml:(未编辑)
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
tools:context=\".ui.slideshow.SlideshowFragment\">
<ImageView
android:id=\"@+id/imageView3\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:src=\"@drawable/ic_launcher_background\"
app:layout_constraintBottom_toBottomOf=\"parent\"
app:layout_constraintEnd_toEndOf=\"parent\" />
</androidx.constraintlayout.widget.ConstraintLayout>
content_main.xml:(未编辑)
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
app:layout_behavior=\"@string/appbar_scrolling_view_behavior\"
tools:showIn=\"@layout/app_bar_main\">
<fragment
android:id=\"@+id/nav_host_fragment_content_main\"
android:name=\"androidx.navigation.fragment.NavHostFragment\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
app:defaultNavHost=\"true\"
app:layout_constraintLeft_toLeftOf=\"parent\"
app:layout_constraintRight_toRightOf=\"parent\"
app:layout_constraintTop_toTopOf=\"parent\"
app:navGraph=\"@navigation/mobile_navigation\" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml:(未编辑)
<?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
android:id=\"@+id/app_bar_main\"
layout=\"@layout/app_bar_main\"
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_main\"
app:menu=\"@menu/activity_main_drawer\" />
</androidx.drawerlayout.widget.DrawerLayout>
activity_main_drawer.xml:(未编辑)
<?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=\"navigation_view\">
<group android:checkableBehavior=\"single\">
<item
android:id=\"@+id/nav_home\"
android:icon=\"@drawable/ic_menu_camera\"
android:title=\"@string/menu_home\" />
<item
android:id=\"@+id/nav_gallery\"
android:icon=\"@drawable/ic_menu_gallery\"
android:title=\"@string/menu_gallery\" />
<item
android:id=\"@+id/nav_slideshow\"
android:icon=\"@drawable/ic_menu_slideshow\"
android:title=\"@string/menu_slideshow\" />
</group>
</menu>
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.example.try3.ui.home.HomeFragment\"
android:label=\"@string/menu_home\"
tools:layout=\"@layout/fragment_home\" />
<fragment
android:id=\"@+id/nav_gallery\"
android:name=\"com.example.try3.ui.gallery.GalleryFragment\"
android:label=\"@string/menu_gallery\"
tools:layout=\"@layout/fragment_gallery\" />
<fragment
android:id=\"@+id/nav_slideshow\"
android:name=\"com.example.try3.ui.slideshow.SlideshowFragment\"
android:label=\"@string/menu_slideshow\"
tools:layout=\"@layout/fragment_slideshow\" />
</navigation>
先感谢您。
标签: java android android-studio android-layout