共享单元转型FragmentTransaction

共享单元转型FragmentTransaction

本文介绍了共享单元转型FragmentTransaction.replace工作(),但不与FragmentTransaction.add工作()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的共享元素转换工作当我使用碎片'的替换的',但我似乎无法使它发挥片段'的添加的'。我使用相同的容器中这两种情况下。

The new Shared Element Transitions works when i use Fragment 'replace' but i can't seem to make it work fragment 'add'. I use the same container in both the cases.

更多细节:

活动 - 布局 - >

Activity - layout->

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffff"
    android:orientation="vertical" >

</FrameLayout>

在推出的活动,我想补充片段1至屏幕

On launch of the Activity, I add Fragment1 to the screen

getFragmentManager().beginTransaction().replace(R.id.container,new TransitionTestFragment1(), "TransitionTestFragment1").commit();

在为片段1的布局景色的单击事件 - >我加Fragment2到屏幕上。我设置了监听器在第一个片段的onCreateView

On a click event for a view in the layout of Fragment1 -> I add Fragment2 to the screen. I set the listener in the first Fragment's onCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
final TransitionSet transitionSet = new TransitionSet();
transitionSet.addTransition(new ChangeImageTransform());
transitionSet.addTransition(new ChangeBounds());
transitionSet.addTransition(new ChangeTransform());
transitionSet.setDuration(300);

View v=inflater.inflate(R.layout.fragment1, null);
final View image=v.findViewById(R.id.image);
image.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        setSharedElementReturnTransition(transitionSet);
        Fragment fragment = new TransitionTestFragment2();
        fragment.setSharedElementEnterTransition(transitionSet);

        FragmentTransaction ft = getFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .addToBackStack("transaction")
                .addSharedElement(image, "MyTransition");
        ft.commit();

    }
});
return v;

}

我有两个片段的布局,这个图像视图

I have this image view in the layouts of both fragments

 <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@drawable/ic_launcher"
        android:transitionName="MyTransition" />

现在,如果我使用的过渡不起作用 FragmentTransaction.add()添加第二个片段,但它的作品,如果我用 FragmentTransaction .replace()来代替。我怎样才能使其与添加工作()?是否有可能呢?

Now, the transition does not work if i use FragmentTransaction.add() to add the second fragment, but it works if i use FragmentTransaction.replace() instead. How can i make it work with add()? Is it possible at all?

推荐答案

我想这是因为新片段被放置在旧片段上。旧片段不被放在了控制器和的onPause (测序方法)没有被调用。它不播放任何过渡,因为旧的片段可能仍然对用户可见(该系统不知道)。

I guess it is because the new fragment is placed on top of the old fragment. The old fragment is not being placed out of the controller and onPause (and sequenced methods) aren't being called. It doesn't play any transitions because the old fragment might still be visible to the user (the system doesn't know that).

在我的(你评论)我增加了一个进入和退出过渡。如果添加它,它甚至动画?如果不是,这是因为陈述的原因可能。

In my answer (where you commented) I added an enter and exit transition. If you add it, does it even animate? If not, it's probably because of the stated reason.

这篇关于共享单元转型FragmentTransaction.replace工作(),但不与FragmentTransaction.add工作()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:34