TL;DR: I am looking for a complete working sample of what I'll refer to as "the Gmail three-fragment animation" scenario. Specifically, we want to start with two fragments, like this:
Upon some UI event (e.g., tapping on something in Fragment B), we want:
- Fragment A to slide off the screen to the left
- Fragment B to slide to the left edge of the screen and shrink to take up the spot vacated by Fragment A
- Fragment C to slide in from the right side of the screen and to take up the spot vacated by Fragment B
And, on a BACK button press, we want that set of operations to be reversed.
Now, I have seen lots of partial implementations; I'll review four of them below. Beyond being incomplete, they all have their issues.
@Reto Meier contributed this popular answer to the same basic question, indicating that you would use setCustomAnimations()
with a FragmentTransaction
. For a two-fragment scenario (e.g., you only see Fragment A initially, and want to replace it with a new Fragment B using animated effects), I am in complete agreement. However:
- Since you can only specify one "in" and one "out" animation, I can't see how you would handle all the different animations required for the three-fragment scenario
- The
<objectAnimator>
in his sample code uses hard-wired positions in pixels, and that would seem to be impractical given varying screen sizes, yetsetCustomAnimations()
requires animation resources, precluding the possibility of defining these things in Java - I am at a loss as to how the object animators for scale tie in with things like
android:layout_weight
in aLinearLayout
for allocating space on a percentage basis - I am at a loss as to how Fragment C is handled at the outset (
GONE
?android:layout_weight
of0
? pre-animated to a scale of 0? something else?)
@Roman Nurik points out that you can animate any property, including ones that you define yourself. That can help solve the issue of the hard-wired positions, at the cost of inventing your own custom layout manager subclass. That helps some, but I'm still baffled by the rest of Reto's solution.
The author of this pastebin entry shows some tantalizing pseudocode, basically saying that all three fragments would reside in the container initially, with Fragment C hidden at the outset via a hide()
transaction operation. We then show()
C and hide()
A when the UI event occurs. However, I don't see how that handles the fact that B changes size. It also relies on the fact that you apparently can add multiple fragments to the same container, and I am not sure whether or not that is reliable behavior over the long term (not to mention it should break findFragmentById()
, though I can live with that).
The author of this blog post indicates that Gmail is not using setCustomAnimations()
at all, but instead directly uses object animators ("you just change left margin of the root view + change width of the right view"). However, this is still a two-fragment solution AFAICT, and the implementation shown once again hard-wires dimensions in pixels.
I will continue plugging away at this, so I may wind up answering this myself someday, but I am really hoping that somebody has worked out the three-fragment solution for this animation scenario and can post the code (or a link thereto). Animations in Android make me want to pull my hair out, and those of you who have seen me know that this is a largely fruitless endeavor.