Fragment ReenterTransition not working. Need help clarifying the various Fragment transitions

半城伤御伤魂 提交于 2019-12-23 08:51:23

问题


I am implementing Fragment transition animations between items in a RecyclerView, and a Fragment showing details of the clicked item. In other words the relatively common...

"Click on a Card in a list and it expands to a detailed view while the rest of the list disappears"

...kind of thing.

The transition from the RecyclerView item to the detailed view is working fine. The shared elements of the item are transitioning to their new state while the rest of the RecyclerView items fade away.

However, when the BackStack is popped the shared elements transition back to their old state, but the other RecyclerView items do not fade back in. They appear instantly at the start of the animation instead, as you can see in this Screen Video

The activity handles quite a few fragments, so I do the transaction in the following generalized method:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setFragment(int fragId, Bundle args, List<Pair> transitionViews,
        String tag, int containerId) {

    // Setup the new fragment and transaction
    Fragment newFragment = FragmentFactory.newFragment(fragId, args);
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(containerId, newFragment, tag);
    fragmentTransaction.addToBackStack(tag);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && transitionViews != null) {

        // Add the shared elements
        for (int i = 0; i < transitionViews.size(); i++) {
            final Pair pair = transitionViews.get(i);
            fragmentTransaction.addSharedElement((View) pair.first, (String) pair.second);
        }

        // Setup the transitions
        Transition transitionMove = TransitionInflater.from(this).inflateTransition(android.R.transition.move);
        Transition transitionFade = TransitionInflater.from(this).inflateTransition(android.R.transition.fade);
    //        transitionFade.setDuration(500);  // Slow down the transition to help see what's happening

        // Apply the relevant transitions to each fragment
        newFragment.setSharedElementEnterTransition(transitionMove);
        newFragment.setEnterTransition(transitionFade);
        newFragment.setExitTransition(transitionFade);
        mCurrentFragment.setExitTransition(transitionFade);
        mCurrentFragment.setReenterTransition(transitionFade);
        mCurrentFragment.setSharedElementReturnTransition(transitionMove);
    }

    fragmentTransaction.commit();
}
  • I have tried playing with allowing/disallowing transition overlap on Enter/Return.
  • I have tried playing with the various transition setting methods for Fragments.
  • I have read through loads of blogs and SO questions on this topic.

I found http://www.androiddesignpatterns.com/2014/12/activity-fragment-transitions-in-android-lollipop-part1.html blog on this topic, and brockoli's sample code very helpful, but have been unable to solve the problem.


Perhaps it is a problem with my understanding of what each transition is for?
Here's how I understand it.

My mCurrentFragment and newFragment have 5 different transition setters each:

  1. setSharedElementEnterTransition Sets the Transition that will be used for shared elements transferred into the content Scene.
  2. setSharedElementReturnTransition Sets the Transition that will be used for shared elements transferred back during a pop of the back stack.
  3. setEnterTransition Sets the Transition that will be used to move Views into the initial scene.
  4. setExitTransition Sets the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.
  5. setReenterTransition Sets the Transition that will be used to move Views in to the scene when returning due to popping a back stack.

When my setFragment method is called, an animation is played transitioning from mCurrentFragment to newFragment with the following properties:

  • The newFragment SharedElementEnterTransition defines how the shared elements will transition into newFragment.
    (In my case the clicked item's CardView expands and one of the TextView's it contains is moved)
  • The newFragment EnterTransition defines how the remaining newFragment child views which are not shared elements will transition onto the screen.
    (In my case a ToolBar fades in at the bottom of the screen. Actually the ToolBar is fading in behind the exiting RecyclerView. Is there any way to swap it so it's in front?)
  • The mCurrentFragment ExitTransition defines how the mCurrentFragment child views which are not shared elements will transition off of the screen.
    (In my case mCurrentFragment only contains the RecyclerView, so the effect is that the rest of the RecyclerView elements fade away in the background)

When the BackStack is popped I would expect the following to occur:

  • The SharedElementReturnTransition for mCurrentFragment defines how the shared elements will transition back into mCurrentFragment.
    (In my case the CardView contracts back down to RecyclerView item size and the TextView it contains is moved back).
  • The ExitTransition for newFragment defines how the newFragment child views which are not shared elements will transition off of the screen.
    (In my case the bottom ToolBar fades out)
  • The ReenterTransition for mCurrentFragment defines how the remaining mCurrentFragment child views which are not shared elements will transition back onto the screen.
    (In my case the other RecyclerView items should fade back in, but this is not happening. They are instantly visible behind the transitioning shared elements).

Have I misunderstood anything?

来源:https://stackoverflow.com/questions/31283413/fragment-reentertransition-not-working-need-help-clarifying-the-various-fragmen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!