Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android new fragment appears below old one during fragment animation

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

like image 342
Vodet Avatar asked Apr 12 '17 20:04

Vodet


People also ask

What is a fragment in Android?

Fragments. A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment’s view hierarchy becomes part of, or attaches to , ...

Do fragments support androidx transitions?

Fragments support AndroidX transitions . While fragments also support framework transitions, we strongly recommend using AndroidX transitions, as they are supported in API levels 14 and higher and contain bug fixes that are not present in older versions of framework transitions.

How do I animate the transition between fragments?

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction. Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).

How do I use motion effects in the fragment API?

The Fragment API provides two ways to use motion effects and transformations to visually connect fragments during navigation. One of these is the Animation Framework, which uses both Animation and Animator. The other is the Transition Framework, which includes shared element transitions.


1 Answers

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

like image 171
Geryson Avatar answered Jan 03 '23 14:01

Geryson