How to use shared element transitions in Navigation Controller

前端 未结 9 522
醉话见心
醉话见心 2021-01-31 08:06

I would like to add a shared elements transition using the navigation architecture components, when navigating to an other fragment. But I have no idea how. Also in the document

相关标签:
9条回答
  • 2021-01-31 08:48

    With the latest library version you can just write the following:

    view.findNavController().navigate(
        R.id.action_firstFragment_to_secondFragment, 
        null,  
        null,
        FragmentNavigator.Extras.Builder().addSharedElements(
            mapOf(
               firstSharedElementView to "firstSharedElementName",
               secondSharedElementView to "secondSharedElementName"
            )
        ).build()
    )
    

    For the transition to work you also have to specify the sharedElementEnterTransition and/or the sharedElementReturnTransition in the destination Fragments onCreateView method just as Xzin explained in his answer.

    0 讨论(0)
  • 2021-01-31 08:50

    I was finally able to get this to work: On Fragment B:

    val transition = TransitionInflater.from(this.activity).inflateTransition(android.R.transition.move)
    
    sharedElementEnterTransition = ChangeBounds().apply {
                enterTransition = transition
            }
    

    Just make sure you have your transition names right in your views and you have NO entertTransition on Fragment B

    0 讨论(0)
  • 2021-01-31 08:51

    I took reference from this github sample https://github.com/serbelga/android_navigation_shared_elements

    cardView.setOnClickListener{
      val extras = FragmentNavigatorExtras(
        imageView to "imageView"
      )
      findNavController().navigate(R.id.detailAction, null, null, extras)
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
    

    It is working properly.

    0 讨论(0)
提交回复
热议问题