【发布时间】: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