Android new fragment appears below old one during fragment animation
Asked Answered
B

2

6

I'm using animation between my fragment :

slide in from left :

<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="600"
    android:propertyName="x"
    android:valueFrom="2000"
    android:valueTo="0"
    android:valueType="floatType"
    android:fillAfter="true"/>
</set>

slide in from right :

<?xml version="1.0" encoding="utf-8"?>
<set>
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="600"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="-400"
        android:valueType="floatType"/>
</set>

This animation move the previous fragment to left and the new one come from the right. I'm using it with a fragment transaction.

transaction.setCustomAnimations(R.animator.slide_in_from_left,R.animator.slide_in_from_right);

I got a trouble when the new fragment come he slide below the older one, for fix it I put an elevation on my new fragment but I would like a solution for API below 21. Is it possible to force the new fragment to be above the older fragment

Bathtub answered 12/4, 2017 at 20:21 Comment(0)
B
-2

If fragment appears below another one is because the fragment container is not the good container type

You need to put fragment inside FrameLayout or RelativeLayout, if you want to test you can paste your fragment layout code at the bottom of the fragment container for have a preview

Bathtub answered 18/4, 2019 at 13:46 Comment(0)
P
1

If I understand correctly, you are using the android:elevation attribute on your new fragment (if not, feel free to write about your method) - altough it only works on Lollipop and above, as you also mentioned. However, based on the linked topic below there are several ways to achieve elevation effect on pre-Lollipop devices.

"android:elevation=" doesn't work on devices pre-Lollipop with compile API21


EDIT AFTER FIRST COMMENT: Okay, sorry for misunderstanding your question... :)

As I searched a little bit in the topic, there were some interesting examples with a new fragment overlapping an older one, they are listed below. The first 2 examples need some special layout design and extensions of existing view classes, but maybe one of them is what you're looking for.

  • In my opinion the prettiest solution would be based on this example: http://swarmnyc.com/whiteboard/android-fragment-animations (the layout does not contain a FrameLayout to replace fragments, it contains the fragment(s) by the <fragment> tag; in the code you can use simple transition animations; you can find the source code at the end of the topic)
  • Here's another one: http://trickyandroid.com/fragments-translate-animation/ (you can see an extension of the native RelativeLayout view class with built-in functions to achieve animation; source code also provided at the end of the topic)

Moreover (without source code) I could suggest you 2 more ways:

  • The animation you search for could be perfectly achieved by using activities for displaying the UI and the overridePendingTransition() method inside the activity after startActivity(intent). However if you really would like to use fragments, maybe you will skip this.
  • The last (and I think the simpliest) solution could be to overlap the old fragment by adding a new fragment instead of replacing it. However I think this is the worst solution because of possible memory usage problems (older fragments won't be removed, just simply faded by newer fragments).

I hope I could help... :)

Paragraph answered 13/4, 2017 at 8:46 Comment(2)
I'm putting an elevation because it's the only way I find for put the new fragment above the previous one during the animation. Links that you show me explain how to reproduce the shadow effect ok the look of the elevation but this will not solve my troubleBathtub
elevation works fine, but when I use backstack(), it reverses the case. so I dynamically change the elevation before the transaction.Stouffer
B
-2

If fragment appears below another one is because the fragment container is not the good container type

You need to put fragment inside FrameLayout or RelativeLayout, if you want to test you can paste your fragment layout code at the bottom of the fragment container for have a preview

Bathtub answered 18/4, 2019 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.