【问题标题】:Reload navigation fragment with bundle arguments and animation, called from within itself使用捆绑参数和动画重新加载导航片段,从自身内部调用
【发布时间】:2019-12-11 15:38:46
【问题描述】:

我有一个导航片段,它接受来自捆绑参数的数据来显示。在用户操作之后,这个片段比导航主机活动更深 3 级。可以选择下一个或上一个(多次),它应该重新加载相同的片段,但使用更新的数据。对于下一个/上一个加载,如果我创建包并在指向自身的操作上调用 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 动画 来获得上一个和下一个按钮的点击?

我正在使用onViewCreated 将数据绑定到视图元素并附加按钮侦听器

【问题讨论】:

  • 我必须为动画做这个吗? stackoverflow.com/q/51856988/1029110
  • 为什么人们使用onActivityCreated?为什么这种方法如此受欢迎?
  • 在代码和问题中更正为 onViewCreated。还是一样的问题
  • @EpicPandaForce 那是因为 Google codelabs 上有一个应用程序可以做到这一点。

标签: android android-fragments kotlin android-jetpack android-jetpack-navigation


【解决方案1】:

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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多