Note that there are some explanatory texts on larger screens.

plurals
  1. POComplete Working Sample of the Gmail Three-Fragment Animation Scenario?
    primarykey
    data
    text
    <p>TL;DR: I am looking for a <strong>complete working sample</strong> of what I'll refer to as "the Gmail three-fragment animation" scenario. Specifically, we want to start with two fragments, like this:</p> <p><img src="https://i.stack.imgur.com/OsysI.png" alt="two fragments"></p> <p>Upon some UI event (e.g., tapping on something in Fragment B), we want:</p> <ul> <li>Fragment A to slide off the screen to the left</li> <li>Fragment B to slide to the left edge of the screen and shrink to take up the spot vacated by Fragment A</li> <li>Fragment C to slide in from the right side of the screen and to take up the spot vacated by Fragment B</li> </ul> <p>And, on a BACK button press, we want that set of operations to be reversed.</p> <p>Now, I have seen lots of partial implementations; I'll review four of them below. Beyond being incomplete, they all have their issues.</p> <hr> <p>@Reto Meier contributed <a href="https://stackoverflow.com/a/4819665/115145">this popular answer</a> to the same basic question, indicating that you would use <code>setCustomAnimations()</code> with a <code>FragmentTransaction</code>. 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:</p> <ul> <li>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</li> <li>The <code>&lt;objectAnimator&gt;</code> in his sample code uses hard-wired positions in pixels, and that would seem to be impractical given varying screen sizes, yet <code>setCustomAnimations()</code> requires animation resources, precluding the possibility of defining these things in Java</li> <li>I am at a loss as to how the object animators for scale tie in with things like <code>android:layout_weight</code> in a <code>LinearLayout</code> for allocating space on a percentage basis</li> <li>I am at a loss as to how Fragment C is handled at the outset (<code>GONE</code>? <code>android:layout_weight</code> of <code>0</code>? pre-animated to a scale of 0? something else?)</li> </ul> <hr> <p>@Roman Nurik points out that <a href="https://stackoverflow.com/a/4936159/115145">you can animate any property</a>, 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.</p> <hr> <p>The author of <a href="http://pastebin.com/gh2eDkFS" rel="noreferrer">this pastebin entry</a> 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 <code>hide()</code> transaction operation. We then <code>show()</code> C and <code>hide()</code> 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 <code>findFragmentById()</code>, though I can live with that).</p> <hr> <p>The author of <a href="http://android.amberfog.com/?p=758" rel="noreferrer">this blog post</a> indicates that Gmail is not using <code>setCustomAnimations()</code> 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.</p> <hr> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload