【问题标题】:Using navigation component Load fragment inside a fragment使用导航组件在片段内加载片段
【发布时间】:2021-10-13 06:41:44
【问题描述】:

在家庭片段中,我将 xml 作为

 <androidx.constraintlayout.widget.ConstraintLayout
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/bottomNavi"
            app:navGraph="@navigation/bottm_navi" />

        <meow.bottomnavigation.MeowBottomNavigation
            android:id="@+id/bottomNavi"
            app:mbn_circleColor="@color/white"
            app:mbn_backgroundBottomColor="@color/app_dark_blue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

我的底部导航是

<?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/bottm_navi"
    app:startDestination="@id/homeBottomOneFragment">

    <fragment
        android:id="@+id/homeBottomOneFragment"
        android:name="com.krassier.customer.ui.home.bottom.HomeBottomOneFragment"
        android:label="fragment_home_bottom_one"
        tools:layout="@layout/fragment_home_bottom_one" />
    <fragment
        android:id="@+id/homeBottomTwoFragment"
        android:name="com.krassier.customer.ui.home.bottom.HomeBottomTwoFragment"
        android:label="fragment_home_bottom_two"
        tools:layout="@layout/fragment_home_bottom_two" />
    <fragment
        android:id="@+id/homeBottomFourFragment"
        android:name="com.krassier.customer.ui.home.bottom.HomeBottomFourFragment"
        android:label="fragment_home_bottom_four"
        tools:layout="@layout/fragment_home_bottom_four" />
    <fragment
        android:id="@+id/homeBottomThreeFragment"
        android:name="com.krassier.customer.ui.home.bottom.HomeBottomThreeFragment"
        android:label="fragment_home_bottom_three"
        tools:layout="@layout/fragment_home_bottom_three" />
    <fragment
        android:id="@+id/homeFragment2"
        android:name="com.krassier.customer.ui.home.HomeFragment"
        android:label="HomeFragment" >
        <action
            android:id="@+id/action_homeFragment2_to_homeBottomOneFragment"
            app:destination="@id/homeBottomOneFragment" />
        <action
            android:id="@+id/action_homeFragment2_to_homeBottomTwoFragment"
            app:destination="@id/homeBottomTwoFragment" />
        <action
            android:id="@+id/action_homeFragment2_to_homeBottomThreeFragment"
            app:destination="@id/homeBottomThreeFragment" />
        <action
            android:id="@+id/action_homeFragment2_to_homeBottomFourFragment"
            app:destination="@id/homeBottomFourFragment" />
    </fragment>
</navigation>

在我写的homefragment中

 findNavController().navigate(R.id.action_homeFragment2_to_homeBottomOneFragment)

当我点击底部导航时,我试图在 homefragment 中加载另一个片段,因为底部导航在 home 片段中,但错误是

java.lang.IllegalArgumentException:导航操作/目的地 com.krassier.customer:id/action_homeFragment2_to_homeBottomOneFragment 无法从当前目的地 Destination(com.krassier.customer:id/homeFragment) label=fragment_home class=com.krassier。 customer.ui.home.HomeFragment

【问题讨论】:

  • 我只是想将一个片段加载到另一个片段中

标签: kotlin android-jetpack-navigation


【解决方案1】:

最后我还是按照常规方式

private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = activity?.supportFragmentManager
        val fragmentTransaction = fragmentManager?.beginTransaction()
        fragmentTransaction?.replace(R.id.homeFragmentContainer, fragment)
        fragmentTransaction?.commit()
    }

【讨论】:

    【解决方案2】:

    既然你在一个片段中,你应该试着用这种方式找到导航控制器。

            val navHostFragment = supportFragmentManager.findFragmentById(R.id. fragment) as NavHostFragment. // the id of your FragmentContainerView
            navController = navHostFragment.navController
    
    

    您应该利用 NavigationUI 库并让它为您处理导航片段更改逻辑

            binding.bottomNavi.setupWithNavController(navController)
    

    【讨论】:

      【解决方案3】:

      你可以试试这个:

          currentDestination?.getAction(direction.actionId)?.run { navigate(direction) }
      }
      
      fun NavController.safeNavigate(
          @IdRes currentDestinationId: Int,
          @IdRes id: Int,
          args: Bundle? = null
      ) {
          if (currentDestinationId == currentDestination?.id) {
              navigate(id, args)
          }
      }
      

      当您多次调用导航方法时会发生此崩溃。

      https://nezspencer.medium.com/navigation-components-a-fix-for-navigation-action-cannot-be-found-in-the-current-destination-95b63e16152e

      【讨论】:

      • 这是一个原因,但很确定他对导航方法的第一次调用也崩溃了,因为他使用了不正确的 navController
      猜你喜欢
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      相关资源
      最近更新 更多