【问题标题】:Android shared element transition between an activity with a fragment to another activity with a fragmentAndroid 在带有片段的 Activity 到带有片段的另一个 Activity 之间共享元素转换
【发布时间】:2016-05-18 08:30:22
【问题描述】:

我有一个包含 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 时,片段的 textview 显示为过渡而不是工具栏(如果我不能在同时)。

【问题讨论】:

  • 请张贴您的尝试?
  • @DaminiMehra SMS 英文格式在这里不起作用,希望你明白人们为什么在这里提问。

标签: android android-layout android-fragments android-animation


【解决方案1】:

【讨论】:

    【解决方案2】:

    面临类似的问题。我的错误在于我做出的那种架构决策。一个活动,每个单独流的多个片段会导致此问题。现在,在流之间切换时我无法进行所需的转换,因为这需要加载一个活动和一个片段。

    首先,将包含活动的片段的背景颜色更改为应用的默认背景颜色。我的应用程序的默认背景颜色是灰色,而容器活动的白色背景看起来非常糟糕。灰色背景看起来不那么糟糕。

    现在,从容器活动中移除默认转换。这可以通过使用来完成 overridePendingTransition(0,0);

    现在事情看起来不那么丑陋了。 这只是一种解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多