共享元素过渡到包含 ViewPager 的片段

Shared element transition to a fragment that contains a ViewPager

我正在尝试执行从 Fragment A 中的 RecyclerView 项目到 Fragment B 的共享元素转换。转换名称设置在两种布局的最外层 CardView 上。我的实现与this示例中的实现基本相同。

一切正常,直到我在 Fragment B 中添加了一个 ViewPager2。

我尝试按照 答案中的步骤操作,但没有成功。

堆栈跟踪:

java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
        at java.util.ArrayList.get(ArrayList.java:437)
        at androidx.fragment.app.FragmentTransitionImpl.setNameOverridesReordered(FragmentTransitionImpl.java:182)
        at androidx.fragment.app.DefaultSpecialEffectsController.startTransitions(DefaultSpecialEffectsController.java:665)
        at androidx.fragment.app.DefaultSpecialEffectsController.executeOperations(DefaultSpecialEffectsController.java:114)
        at androidx.fragment.app.SpecialEffectsController.executePendingOperations(SpecialEffectsController.java:294)
        at androidx.fragment.app.Fragment.run(Fragment.java:2776)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

编辑: 显示我的依赖项以响应建议片段版本需要更新的答案:

ext.kotlin_version = "1.5.10"
ext.navigation_version="2.4.0-alpha02"

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.work:work-runtime-ktx:2.5.0"

// Navigation
implementation "androidx.navigation:navigation-runtime-ktx:$navigation_version"
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"

错误是在 androidx.fragment:fragment:1.3.4 上引入的。要修复它,您需要手动更新到 androidx.fragment:fragment:1.3.5androidx.fragment:fragment-ktx:1.3.5.

根据release notes

Fixed a regression in shared element transitions introduced in Fragment 1.3.4 by aosp/1679887. Fragments now correctly handle transition groups (either set directly via transitionGroup="true" or indirectly via a transitionName or background) and shared elements will no longer throw IndexOutOfBoundsExceptions. (I16484, b/188679569, b/188969304)

添加对 androidx.fragment:fragment-ktx:1.4.0-alpha03 的依赖解决了这个问题。不再需要添加空视图。

release notes 声明如下:

From Fragment 1.3.5: Fixed a regression in shared element transitions introduced in Fragment 1.3.4 by aosp/1679887. Fragments now correctly handle transition groups (either set directly via transitionGroup="true" or indirectly via a transitionName or background) and shared elements will no longer throw IndexOutOfBoundsExceptions. (I16484, b/188679569, b/188969304)