[英]Android shared element transition between an activity with a fragment to another activity with a fragment
我有一个包含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>
此活动包含名为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>
正如您在代码中看到的,两个活动共享工具栏并具有相同的转换名称。 它们包含的片段都有一个textview,我想在过渡中制作动画。 这些文本视图也具有相同的转换名称。 反正有没有这样做? 我试过,到目前为止,我只能在两个活动之间设置工具栏的动画。 如何使片段同时执行共享元素转换?
如果无法做到这一点,当我从MainActivity导航到DetailActivity时,我怎么能这样做呢?片段的文本视图显示为过渡而不是工具栏(如果我不能同时激活活动和片段事务)。
面临类似的问题。 我的错误在于我做出的那种建筑决策。 一个活动,每个单独流程的多个片段会导致此问题。 现在,当在流之间切换时,我无法获得所需的转换,因为这需要加载活动和片段。
首先,将包含活动的片段的背景颜色更改为应用的默认背景颜色。 我的应用程序的默认背景颜色是灰色的,容器活动的白色背景看起来非常糟糕。 灰色背景看起来不那么糟糕。
现在,从容器活动中删除默认转换。 这可以通过使用overridePendingTransition(0,0);
来完成overridePendingTransition(0,0);
现在事情看起来不那么难看了。 这只是一个解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.