片段转换支持25.1.0 with Recyclerview

时间:2017-01-16 13:38:38

标签: android android-fragments android-support-library kotlin

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

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

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

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

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)
...

但如果我禁用"优化"它工作正常。

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
}

1 个答案:

答案 0 :(得分:0)

自25.0.1版以来,它对我来说很好。显示完整代码或至少commonViews。我认为,问题出在你的commonViews中。