Android共享元素转场在一个带有片段的活动与另一个带有片段的活动之间。

7
我有一个包含FragmentA的ActivityA,想要进行共享元素转换到包含FragmentB的ActivityB。
在这两个活动中,共享元素将是工具栏。在片段中,它将是一个文本视图。有没有办法做到这一点?
如果不行,我该如何在这两个活动中的片段之间进行共享元素转换?
非常感谢任何帮助。我已经卡了一段时间了。
好的,我会提供一些代码来澄清。
这里有MainActivity:
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <!-- This is shared with DetailActivity -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:transitionName="sharedToolbar"/>

    </android.support.design.widget.AppBarLayout>

    <!-- This contains MainFragment -->
    <FrameLayout
        android:id="@+id/activity_main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>

这个活动包含名为MainFragment的碎片:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!-- This element is shared with DetailFragment -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Fragment main"
        android:transitionName="sharedFragment"/>

    <!-- When this is clicked show next screen -->
    <Button
        android:id="@+id/fragment_main_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to detail screen"
        android:layout_gravity="bottom|center"/>

</FrameLayout>

现在我想做的是,当我在MainFragment中点击按钮时,我想执行一个片段事务到这个名为DetailActivity的活动:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <!-- This is shared with MainActivity -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:transitionName="sharedToolbar"/>

    </android.support.design.widget.AppBarLayout>

    <!-- This contains DetailFragment -->
    <FrameLayout
        android:id="@+id/activity_detail_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>

这个活动包含一个名为DetailFragment的片段:
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!-- This element is shared with MainFragment -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Fragment detail"
        android:transitionName="sharedFragment"/>

</FrameLayout>

您可以在代码中看到,这两个活动共享工具栏并具有相同的转换名称。它们包含的片段都有一个文本视图,我也想在过渡中对其进行动画处理。这些文本视图也具有相同的转换名称。有没有办法可以做到这一点?我已经尝试过了,到目前为止我只能在这两个活动之间动画显示工具栏。如何使片段同时执行共享元素转换?
如果无法实现上述操作,我该如何才能使从MainActivity导航到DetailActivity时,片段的文本视图出现为转换效果,而不是工具栏(如果不能同时动画化活动和片段交易)?

请发布您尝试的内容。 - Damini Mehra
@DaminiMehra 短信英语格式在这里不起作用,希望你能完全理解为什么人们在这里提问。 - Triode
2个回答

2

0
面对类似的问题。我的错误在于我所做的架构决策。一个活动,多个片段用于每个单独的流导致了这个问题。现在当切换流时,我无法获得期望的过渡效果,因为这需要加载一个活动和一个片段。
首先,将包含活动的片段的背景颜色更改为您应用程序的默认背景颜色。我的应用程序的默认背景颜色是灰色,而容器活动的白色背景看起来非常糟糕。灰色背景看起来不那么糟糕。
现在,从容器活动中删除默认的过渡效果。可以通过使用overridePendingTransition(0,0);来实现。
现在事情看起来不那么丑陋了。 这只是一个权宜之计。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接