Переход на общий элемент Android между двумя действиями не работает
В моем приложении я пытаюсь использовать недавно введенный элемент обмена между действиями. Все работает как шарм, если общий элемент имеет фиксированную позицию (например, android:layout_gravity="top"
), но проблема возникает, когда представление привязано.
Мое первое действие выглядит следующим образом:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:auto="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/play_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="24dp"/>
</android.support.design.widget.CoordinatorLayout>
Мое второе действие выглядит следующим образом
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:auto="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="10dp"
android:src="@drawable/ic_action_play"
auto:layout_anchor="@+id/appbar"
android:transitionName="fab_button"
auto:layout_anchorGravity="bottom|right" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp">
...
</android.support.design.widget.AppBarLayout>
...
</android.support.design.widget.CoordinatorLayout>
Используемый мной код выглядит следующим образом:
Intent intent = ...;
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, view, "fab_button");
startActivity(intent, options.toBundle());
Если я использую атрибуты layout_anchor
и layout_anchorGravity
, переход между двумя FAB выполняется без анимации. Если второй FAB имеет фиксированное положение, он работает отлично. Что я делаю неправильно?
Ответы
Ответ 1
Это может быть немного поздно, но я нашел способ решить эту проблему. Вы должны обернуть свой общий элемент в макет и поместить привязку на этот макет:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
auto:layout_anchor="@+id/appbar"
auto:layout_anchorGravity="bottom|right">
<android.support.design.widget.FloatingActionButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="10dp"
android:src="@drawable/ic_action_play"
android:transitionName="fab_button" />
<FrameLayout/>
Ответ 2
Действия Общие элементы Переходы:
Выполните шаги.
Я думаю, что вы не следуете второму шагу. Вы указали android:transitionName="fab_button"
во втором Activity
, но не указаны в First Activity
.
XML
Первого Activity
(добавлено android:transitionName="fab_button"
):
<android.support.design.widget.FloatingActionButton
android:transitionName="fab_button"
android:id="@+id/play_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="24dp"/>
Возможно, майские ссылки помогут вам.
Ответ 3
У вас также должно быть имя перехода в процессе приема. Проверьте мой код.
Первая активность
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_register"
android:transitionName="@string/transition"/>
</android.support.design.widget.CoordinatorLayout>
И вторая активность xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:transitionName="@string/transition"
tools:context="com.example.shoppingappdemo.RegiserActivity">