fragment to fragment animation with shared element don't work as expected

寵の児 提交于 2019-12-18 09:38:18

问题


In an AppCompatActivity with a RecyclerView in a fragment (myFragment1), viewholders and so on, so if I click on an item, then myFragment2 will show and display the detail of the item. In this phase, I want to create an animation between the two fragments with shared elements.
Steps to reproduce the behaviour:

import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
[...]
public class AppActivity extends AppCompatActivity

myFragment1.setSharedElementReturnTransition(new MyNewTransition());
myFragment1.setSharedElementEnterTransition(new MyNewTransition());
[...]
a) getSupportFragmentManager().beginTransaction()
                    .addSharedElement(myImage, "mytransition")
                    .replace(R.id.recycler_view_container, myFragment2)
                    .commit();

b) getSupportFragmentManager().beginTransaction()
                    .addSharedElement(myImage, "mytransition")
                    .add(R.id.recycler_view_container, myFragment2)
                    .hide(myFragment1) 
                     commit();

In the (a) code, the image transition is correct and animation occurs perfectly between the shared elements, but I don't need this. In the (b) code, the only difference is that myFragment1 is being hidden and the myFragment2 is added, in this case both the enter transition and the return transition are broken and no animation occurs.
I need the (b) code because "replace"-ing the fragments will destroy the myFragment1 and rebuilding it is a heavy-load process. I think this is a bug with androidx libraries

EDIT: I have build a sample based on the demonstration of Bryan Herbst:
FragmentTransitionSample and re-adapted for androidx, here are the sources.


回答1:


Try adding setReorderingAllowed(true)

For example:

getSupportFragmentManager().beginTransaction() 
                .setReorderingAllowed(true)
                .addSharedElement(myImage, "mytransition") 
                .add(R.id.recycler_view_container, myFragment2) 
                .hide(myFragment1)  
                 commit(); 


来源:https://stackoverflow.com/questions/53179476/fragment-to-fragment-animation-with-shared-element-dont-work-as-expected

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