带有共享元素的片段到片段动画无法按预期工作 [英] fragment to fragment animation with shared element don't work as expected
问题描述
在一个AppCompatActivity
中,其中一个RecyclerView
包含在一个片段(myFragment1
),viewholders
等中,因此,如果单击某个项目,则myFragment2
将显示并显示该项目的详细信息.在此阶段,我想在两个片段之间创建一个具有共享元素的动画.
重现该行为的步骤:
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();
在(a)代码中,图像过渡是正确的,并且动画在共享元素之间完美地发生,但是我不需要这样做.
在(b)代码中,唯一的区别是myFragment1
被隐藏并添加了myFragment2
,在这种情况下,entry过渡和return过渡都被破坏了,并且没有动画发生.
我需要使用(b)代码,因为替换"片段将破坏myFragment1
并重建它是一个繁重的过程.
我认为这是androidx库的错误
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
我基于Bryan Herbst的演示构建了一个示例:
FragmentTransitionSample 并进行了重新调整androidx,此处是来源.
I have build a sample based on the demonstration of Bryan Herbst:
FragmentTransitionSample and re-adapted for androidx, here are the sources.
推荐答案
尝试添加setReorderingAllowed(true)
例如:
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.addSharedElement(myImage, "mytransition")
.add(R.id.recycler_view_container, myFragment2)
.hide(myFragment1)
commit();
这篇关于带有共享元素的片段到片段动画无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!