片段转换支持 25.1.0 与 Recyclerview

Fragment transition support 25.1.0 with Recyclerview

我正在使用片段转换来为来自回收器视图的视图设置动画,使用 25.0.1 支持库并且转换工作正常以推送和弹出目标片段。

我用来生成交易的代码是:

supportFragmentManager.beginTransaction()
    .replaceAddToBackStack(R.id.content, fragment)
    .addSharedElement(fragment, commonViews)
    .commit()

现在我正在尝试更新到支持库的 25.1.0 版本,但流行动画崩溃了,因为转换没有找到原始视图,并且崩溃了:

E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.support.v4.app.FragmentTransitionCompat21.setNameOverridesOptimized(FragmentTransitionCompat21.java:339)
at android.support.v4.app.FragmentTransition.configureTransitionsOptimized(FragmentTransition.java:232)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:111)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2143)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:822)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:775)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:178)
at App.ui.base.BaseActivity.onBackPressed(BaseActivity.kt:23)
...

但是如果我禁用 "optimizations" 它就可以正常工作。

supportFragmentManager.beginTransaction()
    .replaceAddToBackStack(R.id.content, fragment)
    .addSharedElement(fragment, commonViews)
    .setAllowOptimization(false)
    .commit()

我做错了什么?

编辑: 我用来包装共享元素的扩展:

.replaceAddToBackStack

fun<T : Fragment> FragmentTransaction.replaceAddToBackStack(containerView: Int, fragment: T, tag: String? = null) : FragmentTransaction {
  return replace(containerView, fragment, tag).addToBackStack(fragment.toString())
}

.addSharedElement

fun<T: Fragment, V: View> FragmentTransaction
    .addSharedElement(destination: T?, sharedViews: List<V>?,
                      sharedTransform: TransitionSet? = null,
                      enterTransition: Transition? = null,
                      exitTransition: Transition? = null
                      ) : FragmentTransaction {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    return this
  }
  if (destination == null || sharedViews == null) {
    return this
  }

  val fade = Fade()
  fade.duration = 150
  destination.enterTransition = enterTransition ?: fade
  destination.allowEnterTransitionOverlap = false
  destination.allowReturnTransitionOverlap = false
  destination.exitTransition = exitTransition ?: fade
  destination.sharedElementEnterTransition = sharedTransform ?: DetailsTransition()
  destination.sharedElementReturnTransition = sharedTransform ?: DetailsTransition()

  sharedViews.forEach {
    this.addSharedElement(it, it.transitionName)
  }
  return this
}

从 25.0.1 版本开始它对我来说很好用。显示您的完整代码或至少 commonViews。我认为,问题出在您的 commonViews 上。