使用 NavController 从片段导航到另一个片段
Posted
技术标签:
【中文标题】使用 NavController 从片段导航到另一个片段【英文标题】:Navigate from fragment to another using NavController 【发布时间】:2021-05-02 20:21:42 【问题描述】:我正在尝试应用我看到的关于带有片段和 MVVM“新组件”的导航抽屉活动的课程,在这个简单的应用程序中,我尝试使用三个片段在它们之间导航
navController.addOnDestinationChangedListener
并显示toast消息,但似乎代码不起作用,我不知道原因
这是我的代码
public class MainActivity extends AppCompatActivity
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
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 = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// 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)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener()
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments)
int menuId = destination.getId();
switch (menuId)
case R.id.nav_gallery:
fab.hide();
Toast.makeText(getApplicationContext(),"Gallery",Toast.LENGTH_LONG).show();
fragmentTransaction.replace(R.id.homeFragment,new GalleryFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
NavGraph navGraph = new NavGraph()
// controller.getGraph().clear();
// controller.navigate(R.id.nav_gallery);
break;
case R.id.nav_slideshow:
fab.hide();
Toast.makeText(getApplicationContext(),"Slideshow",Toast.LENGTH_LONG).show();
break;
);
@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);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
</androidx.drawerlayout.widget.DrawerLayout>
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_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_
android:layout_
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>
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.test.dummyappv3.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_gallery"
android:name="com.test.dummyappv3.ui.gallery.GalleryFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/nav_slideshow"
android:name="com.test.dummyappv3.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />
</navigation>
在寻找解决方案时,我发现了这个answer,并尝试使用这样的控制器
controller.getGraph().clear();
controller.navigate(R.id.nav_gallery);
而不是使用片段事务,但它也不起作用
【问题讨论】:
【参考方案1】:好的,你需要了解两件事。
NavigationController 与 FragmentManager 没有任何共同之处,只是它也可以交换 Fragment。
应用您的课程,您需要决定使用 FragmentManager 或 NavigationController。
同时使用这两者不仅会让自己感到困惑,而且可能会使应用程序本身崩溃。
【讨论】:
我尝试了两种方法,但没有一种方法可以工作,新方法是使用 NavigationController 我添加了这一行Navigation.findNavController( MainActivity.this, R.id.nav_host_fragment).navigate(R.id.nav_gallery);
它应该真的可以工作,但不幸的是我可以'不知道这个项目有什么问题【参考方案2】:
这里的问题
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
将此包含从此处移至 NavigationView 的顶部
<?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_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
那么一切都会好起来的
【讨论】:
以上是关于使用 NavController 从片段导航到另一个片段的主要内容,如果未能解决你的问题,请参考以下文章
IllegalArgumentException:此 NavController 未知导航目的地 xxx