fragments, android:zAdjustment (z order) and animations

前端 未结 3 1551
小蘑菇
小蘑菇 2021-02-04 00:17

I\'m using the support library. Now, I want to have a fragment shifting in from the bottom, moving OVER the previous one.

For this I use this to keep the previous fragme

相关标签:
3条回答
  • 2021-02-04 00:21

    I've also got stuck with that problem. So instead of using transaction.replace(containerId, newFragment) I've created two containers for fragments and now my code looks like this one

    Add first fragment:

    transaction.add(containerId1, firstFragment).commit();
    

    Add second fragment with animation over the first one:

    findViewById(containerId2).bringToFront();
    transaction.setCustomAnimations(R.anim.slide_in_up,
     R.anim.stay).remove(oldFragment).add(containerId2, newFragment).commit()
    
    0 讨论(0)
  • 2021-02-04 00:30

    You can override the onCreateAnimation method and for any animations you can check what animation is currently running and if you need it to be on top, set the Z-axis from there.

    override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation? {
        if (nextAnim == R.anim.enter_from_right || nextAnim == R.anim.exit_to_right) {
            ViewCompat.setTranslationZ(view, 1f)
        } else {
            ViewCompat.setTranslationZ(view, 0f)
        }
    
        return super.onCreateAnimation(transit, enter, nextAnim)
    }
    

    Recommend implementing this as a base Fragment class for all your fragments.

    0 讨论(0)
  • 2021-02-04 00:31

    According to this google group thread Z adjustment only works for window animations.

    "The Z adjustment only works for window animations. I thought this was documented, but apparently not." -- Dianne Hackborn (Android framework engineer)

    0 讨论(0)
提交回复
热议问题