处理从父片段嵌套到 Tablayout 片段中的图形

Handle Graphs nesting from Parent Fragment into Tablayout Fragments

当我导航到 Tab 片段时(具有带有 viewpager 的 tablayout) 我需要将 tablayout 片段导航图与主导航图嵌套。

这是代码。

Main_Navigation_Graph

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

     <include app:graph="@navigation/tab_navigation" />

    <fragment
        android:id="@+id/C1_fragment"
        android:name="com.example.nestednavigationgraphexample.Fragment_C1"
        android:label="C1"
        tools:layout="@layout/fragment_c1">
        <action
            android:id="@+id/C1_to_C2_fragment"
            app:destination="@id/C2_fragment" />
    </fragment>
    <fragment
        android:id="@+id/C2_fragment"
        android:name="com.example.nestednavigationgraphexample.Fragment_C2"
        android:label="C2"
        tools:layout="@layout/fragment_c2" >
        <action
            android:id="@+id/action_C2_fragment_to_tabFragment"
            app:destination="@id/tabFragment" />
    </fragment>
    <fragment
        android:id="@+id/tabFragment"
        android:name="com.example.nestednavigationgraphexample.Tab_Fragment"
        android:label="tabFragment" />


</navigation>

Tab_Navigation_Graph

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

    <fragment
        android:id="@+id/cardFragment"
        android:name="com.example.nestednavigationgraphexample.CardFragment"
        android:label="CardFragment" >
        <action
            android:id="@+id/action_cardFragment_to_fragment_Tab_Sub"
            app:destination="@id/fragment_Tab_Sub" />
    </fragment>
    <fragment
        android:id="@+id/fragment_Tab_Sub"
        android:name="com.example.nestednavigationgraphexample.Fragment_Tab_Sub"
        android:label="fragment_fragment__tab__sub"
        tools:layout="@layout/fragment_fragment__tab__sub" />
</navigation>

CardFragment

class CardFragment : Fragment(),View.OnClickListener  {
    private var counter: Int? = null
    var navController: NavController?=null

    private val COLOR_MAP = intArrayOf(
        R.color.red_100, R.color.red_300, R.color.red_500, R.color.red_700, R.color.blue_100,
        R.color.blue_300, R.color.blue_500, R.color.blue_700, R.color.green_100, R.color.green_300,
        R.color.green_500, R.color.green_700
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            counter = arguments!!.getInt(ARG_COUNT)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? { // Inflate the layout for this fragment
        return inflater.inflate(R.layout.card_fragment, container, false)
    }

   override fun onViewCreated(
        view: View,
        savedInstanceState: Bundle?
    ) {
        super.onViewCreated(view, savedInstanceState)
        view.setBackgroundColor(ContextCompat.getColor(context!!, COLOR_MAP[counter!!]))
        val textViewCounter = view.findViewById<TextView>(R.id.tv_counter)
        textViewCounter.text = "Fragment No " + (counter!! + 1)
        view.findViewById<Button>(R.id.button).setOnClickListener(this)
        navController= Navigation.findNavController(activity!!,R.id.nav_host_fragment)
   }

    companion object {
        private const val ARG_COUNT = "param1"
        fun newInstance(counter: Int?): CardFragment {
            val fragment = CardFragment()
            val args = Bundle()
            args.putInt(ARG_COUNT, counter!!)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onClick(v: View) {
        when(v!!.id){

            R.id.button->
                navController!!.navigate(R.id.action_cardFragment_to_fragment_Tab_Sub)
        }   
    }
}

单击卡片片段中的按钮时发生以下崩溃

java.lang.IllegalArgumentException: navigation destination com.example.nestednavigationgraphexample:id/action_cardFragment_to_fragment_Tab_Sub is unknown to this NavController

我的选项卡布局有 2 个 CardFragment 实例。我不知道如何嵌套这两个图。请帮助我解决问题,或者如果有人可以推荐一个很好的工作示例,我将不胜感激。提前致谢。

很高兴收到错误 logcat,但我认为您还没有提供来自 TabFragment 的操作。这是需要包含在您的 tabFragment 中的操作,例如此代码:

 <fragment
        android:id="@+id/tabFragment"
        android:name="com.example.nestednavigationgraphexample.Tab_Fragment"
        android:label="tabFragment">

        <action
            android:id="@+id/action_cardFragment_to_fragment_Tab_Sub"
            app:destination="@id/tabSubFragment" />

    </fragment>

    <fragment
        android:id="@+id/tabSubFragment"
        android:name="com.example.nestednavigationgraphexample.Fragment_Tab_Sub"
        tools:layout="@layout/fragment_fragment__tab__sub"
        android:label="tabSubFragment" />