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

Dei*_*son 7 android android-animation android-layout android-fragments

我有一个包含FragmentA的ActivityA,并希望将共享元素转换为包含FragmentB的ActivityB.

在活动中,共享元素将是工具栏.在片段中它将是一个textview.反正有没有这样做?

如果不是,我怎么能在活动中的两个片段之间进行共享元素转换?

在此先感谢您的帮助.我被困了一段时间.

好的,我会提供一些代码来澄清.

这里我有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>
Run Code Online (Sandbox Code Playgroud)

此活动包含名为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>
Run Code Online (Sandbox Code Playgroud)

现在我要做的是当我点击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>
Run Code Online (Sandbox Code Playgroud)

此活动包含名为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>
Run Code Online (Sandbox Code Playgroud)

正如您在代码中看到的,两个活动共享工具栏并具有相同的转换名称.它们包含的片段都有一个textview,我想在过渡中制作动画.这些文本视图也具有相同的转换名称.反正有没有这样做?我试过,到目前为止,我只能在两个活动之间设置工具栏的动画.如何使片段同时执行共享元素转换?

如果这个不能做,我怎么会让它所以当我从MainActivity到DetailActivity浏览这些碎片的TextView的是什么显示为过渡,以不工具栏(如果我不能在同一时间活动的动画片段和交易).