使用包参数和动画重新加载导航片段,从自身内部调用

Reload navigation fragment with bundle arguments and animation, called from within itself

我有一个导航片段,它接受要从包参数中显示的数据。用户操作后,此片段比导航主机 activity 深 3 级。可以选择进入下一个或上一个(多次),这应该重新加载相同的片段,但使用更新的数据。对于 next/prev 加载,如果我创建包并在指向自身的操作上调用 Navigation.findNavController(it).navigate,它只会继续向堆栈添加相同的片段。

在文件 app:navGraph="@navigation/mobile_navigation"<navigation> 标签内以及其他片段

   <fragment
        android:id="@+id/questionFragment"
        android:name="activities.qacenter.QuestionFragment"
        android:label="fragment_question_center"
        tools:layout="@layout/fragment_question">
        <argument
            android:name="questionObj"
            app:argType="model.Question" />
        <action
            android:id="@+id/action_questionFragment_self"
            app:destination="@id/questionFragment" />
    </fragment>

内部转发按钮的点击侦听器 override fun onViewCreated(view: View, savedInstanceState: Bundle?)

forward.setOnClickListener {
        if (thisQ?.questionNo != null) {
            val curQNo = thisQ?.questionNo!!
            val curLevelNo = thisQ?.qOfLevel!!
            val totalQCount = thisQ?.totalQCount!!
            if (curQNo < totalQCount) {
                val bundle = Bundle()
                val etActivity = context as eTLanding
                val wholeQSet =  etActivity.getSelectionSet()!!
                var questionSet = wholeQSet[curLevelNo-1].questions!!
                var nextQuestion = questionSet[curQNo]
                bundle.putSerializable("questionObj", nextQuestion)
                Navigation.findNavController(it).navigate(R.id.action_questionFragment_self, bundle)
            } else {
                forward.isEnabled = false
            }
        }
    }

我如何用popToLeft/Right替换当前显示的片段popToLeft/Right动画上一个和下一个按钮点击?

我正在使用 onViewCreated 绑定数据以查看元素并附加按钮侦听器

This worked 稍作调整,例如根据我的需要添加更多参数

internal fun navigateWithAnimation(actionId: Int, beginStackFrag: Int, anyView: View, isBackNav: Boolean, bundle: Bundle) {

        val navBuilder = NavOptions.Builder()

        val navController = Navigation.findNavController(anyView)
        // val currNavigationNode = navController.graph.findNode(beginStackFrag)

        if (isBackNav) {
            navBuilder.setEnterAnim(R.anim.slide_out_right)
        } else {
            navBuilder.setEnterAnim(R.anim.slide_out_left)
        }
        navBuilder.setPopUpTo(beginStackFrag, false)

        navController.navigate(actionId, bundle, navBuilder.build())
    }

备选方案