简体   繁体   中英

How to use Fragment transition on Android 8 (API 26, Oreo)

I am using a FragmentManager to replace one fragment with another with a shared element transition (two elements are shared). Here is the code (in Kotlin):

private fun showList(edited: EditedAlarm) {
    fragmentManager.findFragmentById(R.id.main_fragment_container)?.apply{
        exitTransition = Fade()
    }

    // create a new Fragment and set transitions
    val listFragment = AlarmsListFragment().apply {
        sharedElementEnterTransition = moveTransition()
        enterTransition = Fade()
    }

    fragmentManager.beginTransaction()
            .addSharedElement(viewHolder.digitalClock(), "clock" + viewHolder.alarmId())
            .addSharedElement(viewHolder.container(), "onOff" + viewHolder.alarmId())
            .replace(R.id.main_fragment_container, listFragment)
            .commit()
}

Transitions itself is defined like this:

    private fun moveTransition(): TransitionSet {
    return TransitionSet().lollipop {
        ordering = TransitionSet.ORDERING_TOGETHER
        addTransition(ChangeBounds())
        addTransition(ChangeTransform())
    }
}

This code works on my Moto Z2 Play (Android close to AOSP) and on my Motorola One (Android ONE) only if:

  • Target API is lower than 26
  • OR Support v4 Library Fragments are used (with minor adjustments - use supportFragmentManager)

However, id does not work properly if target API is 26 or higher. If target API is set to 26, shared elements are not animated . Instead, both shared elements jump to their end positions while everything else is animated.

How can I make it work without using the Support Library? Am I missing some attribute which has to be set?

After some reasearch and thanks to the comment from Eugen Pechanec, I have concluded that using the v4 support library is the way to go . Platform fragments are deprecated in API Versions 28, so we will have to mirgate sooner or later.

I cannot say that support library works flawlessly, but at least shared elements transitions are being animated.

I was not able to make it work without the library, having invested quite a lot of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM