没有fragmentTransaction.replace()的Android片段共享元素过渡

时间:2019-03-25 14:49:33

标签: android fragment-transitions

我无法完全完成片段初始化的转换:

 val fragmentTransaction = fragmentManager.beginTransaction()
    val fragments = fragmentManager.fragments
    if (fragments.isNotEmpty())
        for (oldFragment in fragments)
            if (oldFragment.isAdded && (!second || oldFragment.tag?.substring(0, userId.length)!!.contains(fragmentTag)))
                fragmentTransaction.hide(oldFragment)

    //create or show
    var fragment = fragmentManager.findFragmentByTag(fragmentTag + userId) as? F_Base

    if (fragment == null || mKillFrags.contains(fragmentTag) || killFragment) {
        if (fragment != null)
            fragmentTransaction.remove(fragment)

        fragment = createFragmentByTag(fragmentTag, bundle)

        if (fragment == null) {
            UIUtils.broadcastMessage(R.string.error)

            return false
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            fragment.sharedElementEnterTransition = DetailsTransition()
            fragment.sharedElementReturnTransition = null
            fragment.enterTransition = Fade()
            fragment.exitTransition = null
        }

        //todo test animation
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && sharedView.isNotEmpty()) {
            for (view in sharedView)
                fragmentTransaction.addSharedElement(view, view.transitionName.substring(0, view.transitionName.lastIndexOf("_")))
            fragmentTransaction.replace(content.id, fragment, fragmentTag)
        } else
        fragmentTransaction.add(content.id, fragment, fragmentTag + userId)
    } else {
        fragmentTransaction.show(fragment)
    }
    fragmentTransaction.commitAllowingStateLoss()

在这种情况下,过渡有效,因为片段的初始化经过fragmentTransaction.replace(),但是我不想销毁当前的片段,除非我离开fragmentTransaction.add(),否则不会进行过渡。 / p>

您有解决方案吗?

0 个答案:

没有答案