Search code examples
androidandroid-fragmentsandroid-recyclerviewuinavigationcontrollertransition

NavController sharedTransition RecyclerView item to Fragment not working


So I've read all the articles and examples on sharedTransitions I could find and it seemed simple enough, but for some reason it doesn't work for me.

This is my RecyclerView.Item layout:

<androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/LibraryItem">

        <ImageView
                android:id="@+id/library_note_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:transitionName="@string/library_item_transition"
                style="@style/LibraryImage"/>
    </androidx.cardview.widget.CardView>

This is the onClickListener action for the RecyclerView items in my first Fragment:

private fun startEditNoteFragment(view: View) {
            val action = LibraryFragmentDirections.actionEditNote()
            val extras = FragmentNavigatorExtras(view to view.transitionName)

            findNavController().navigate(action, extras)
        }

Note: I tried using the destination Fragment id instead of the action, also tried making the action transition animations from none to something else. Nothing worked.

This is the edit Fragment (the one we're transitioning to) layout:

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent" 
        android:layout_height="match_parent">

    <com.ink.edit.drawing.view.PathRenderingView
            android:id="@+id/pathRenderingView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:transitionName="@string/library_item_transition"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

And here's the transition code in the Fragment:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
    }

Note: I also tried doing postponeEnterTransition() in onCreate and startPostponedEnterTransition() in onCreateView and still nothing.

I'm testing on Pixel 3 with Android 9, the project's minSdkVersion is 21, buildToolsVersion is 28.0.3. Navigation library dependencies are navigation-fragment-ktx:2.0.0 and navigation-ui-ktx:2.0.0


Can anyone tell me what I'm doing wrong or missing?


Solution

  • Transition names must be unique on the view hierarchy and it looks like you are adding many elements with the same transition name (as many as items you have in the recycler view)