我有一个导航片段,该片段接受要从包参数显示的数据。在用户操作之后,此片段比nav主机活动深3级。可以选择下一个或上一个(多次),该选项应重新加载相同的片段,但使用新的数据。对于下一个/上一个加载,如果我创建bundle并在指向自身的操作上调用Navigation.findNavController(it).navigate
,它只会继续将相同的片段添加到堆栈中。
文件<navigation>
的{{1}}标签内以及其他片段
app:navGraph="@navigation/mobile_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?)
我如何用popToLeft / Right animation 来替换当前显示的片段?
我正在使用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
}
}
}
将数据绑定到视图元素并附加按钮侦听器
答案 0 :(得分:0)
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())
}
替代How to add animation transition to NavigationUI in android?