0

我的应用程序使用导航组件,我拥有CreditFragment并且它使用TabLayoutwith ViewPager2,它包含CreditTabFragment,当我在CreditTabFragment我想要导航到NeedHelpFragmentusing时findNavController()

我怎样才能让导航控制器知道我在上creditTabFragment

我试图创建一个childFragmentManager但没有成功

导航到 NeedHelpFragment 的按钮的 ClickListener

Sweta Jain 显示此错误,我无法从答案中添加 Navigation.findNavController().navigate(action)

4

2 回答 2

0

您可以尝试这样做:

import androidx.navigation.Navigation

    buttonNeedHelp.setOnClickListener {it: View!
     val action =
             CreditTabFragmentDirections.actionCreditTabFragmentToNeedHelpFragment(args) // add args in the bracket, as needed
            Navigation.findNavController(it)
                .navigate(action)
    Navigation.findNavController().navigate(action)
    }

或者,如果您没有参数,请尝试使用导航图中的操作 ID,

buttonNeedHelp.setOnClickListener {it: View!
        Navigation.findNavController(it)
         .navigate(R.id.action_creditTabFragment_to_needHelpFragment)
}
于 2021-10-06T09:00:53.873 回答
0

我在设置视图寻呼机适配器的地方使用了子片段管理器: viewPager.adapter = SectionsPageAdapter(childFragmentManager, lifecycle) 有了它,您将能够使用导航,注意:导航必须在父片段中,而不是在选项卡布局中。

于 2021-11-16T13:56:12.490 回答