NavController sharedTransition RecyclerView 项目到 Fragment 不工作
NavController sharedTransition RecyclerView item to Fragment not working
所以我阅读了我能找到的关于 sharedTransitions
的所有文章和示例,它看起来很简单,但由于某些原因它对我不起作用。
这是我的 RecyclerView.Item
布局:
<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>
这是我第一个 Fragment
中 RecyclerView
项的 onClickListener
操作:
private fun startEditNoteFragment(view: View) {
val action = LibraryFragmentDirections.actionEditNote()
val extras = FragmentNavigatorExtras(view to view.transitionName)
findNavController().navigate(action, extras)
}
注意:我尝试使用目标 Fragment
id 而不是 action
,还尝试制作从 none
到其他内容的动作过渡动画。没有任何效果。
这是编辑 Fragment
(我们正在过渡到的)布局:
<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>
这里是 Fragment
中的转换代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
}
注意:我也尝试在 onCreate
中执行 postponeEnterTransition()
并在 onCreateView
中执行 startPostponedEnterTransition()
,但仍然没有。
我正在使用 Android 9 在 Pixel 3 上进行测试,项目的 minSdkVersion
是 21,buildToolsVersion
是 28.0.3。导航库依赖项是 navigation-fragment-ktx:2.0.0
和 navigation-ui-ktx:2.0.0
谁能告诉我我做错了什么或遗漏了什么?
转换名称在视图层次结构中必须是唯一的,看起来您正在添加许多具有相同转换名称的元素(与回收器视图中的项目一样多)
所以我阅读了我能找到的关于 sharedTransitions
的所有文章和示例,它看起来很简单,但由于某些原因它对我不起作用。
这是我的 RecyclerView.Item
布局:
<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>
这是我第一个 Fragment
中 RecyclerView
项的 onClickListener
操作:
private fun startEditNoteFragment(view: View) {
val action = LibraryFragmentDirections.actionEditNote()
val extras = FragmentNavigatorExtras(view to view.transitionName)
findNavController().navigate(action, extras)
}
注意:我尝试使用目标 Fragment
id 而不是 action
,还尝试制作从 none
到其他内容的动作过渡动画。没有任何效果。
这是编辑 Fragment
(我们正在过渡到的)布局:
<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>
这里是 Fragment
中的转换代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
}
注意:我也尝试在 onCreate
中执行 postponeEnterTransition()
并在 onCreateView
中执行 startPostponedEnterTransition()
,但仍然没有。
我正在使用 Android 9 在 Pixel 3 上进行测试,项目的 minSdkVersion
是 21,buildToolsVersion
是 28.0.3。导航库依赖项是 navigation-fragment-ktx:2.0.0
和 navigation-ui-ktx:2.0.0
谁能告诉我我做错了什么或遗漏了什么?
转换名称在视图层次结构中必须是唯一的,看起来您正在添加许多具有相同转换名称的元素(与回收器视图中的项目一样多)