android - 共享元素过渡越过导航栏

标签 android animation shared-element-transition

我正在尝试使用共享元素转换在两个屏幕之间制作动画。下面是我的代码的相关部分(可能还有更多不相关的部分),但为了节省空间,我删除了其中的一些部分。

activity_main.xml:

<android.support.v4.widget.DrawerLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout ... >
            ...
        </android.support.design.widget.AppBarLayout>

        <fragment 
            android:id="@+id/fragment"
            android:name=".MainFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:layout="@layout/fragment_main" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView ... />

</android.support.v4.widget.DrawerLayout>

fragment _main.xml:

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="8dp"
    android:scrollbars="vertical"
    tools:context=".MainFragment"
    tools:showIn="@layout/activity_main" />

RecyclerView 中的每个项目都包含一个 CardView 作为 Root View :

card_item.xml:

<android.support.v7.widget.CardView  
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.gms.maps.MapView 
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            map:liteMode="true" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/map"
            android:transitionName="@string/transition_title" />

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/job_title"
            android:transitionName="@string/transition_rate" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/job_rate"
            android:transitionName="@string/transition_description" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

在 RecyclerView 的 ViewHolder 中,我设置了 onClickListener:

mCardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(mContext, SecondActivity.class);

        View title = mCardView.findViewById(R.id.title);
        View rate = mCardView.findViewById(R.id.rate);
        View description = mCardView.findViewById(R.id.description);

        // Use fancy animations for API 21+
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mContext.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(mActivity,
                        Pair.create(title, mContext.getString(R.string.transition_title)),
                        Pair.create(rate, mContext.getString(R.string.transition_rate)),
                        Pair.create(description, mContext.getString(R.string.transition_description))).toBundle()
                );
            } else {
                mContext.startActivity(intent);
            }
        }
    });

问题是,如果我点击 TextViews 稍微离开屏幕的卡片,它们会通过导航栏/导航栏上方进行动画处理:

text overlapping navigation bar

我怎样才能防止这种情况发生,以便 TextViews 在导航栏“下方”进行动画处理?

最佳答案

您也可以将导航栏添加为共享元素

View navigationBar = mContext.findViewById(android.R.id.navigationBarBackground);

并与 Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME 创建配对,

Pair.create(navigationBar, Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME)

关于android - 共享元素过渡越过导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34767794/

相关文章:

android - 单击工具栏导航图标会使应用程序崩溃

java - 如何在 java 中调用用 kotlin 类编写的函数?

android - 删除 RecyclerView 中的项目 - Android

android - 使用 CollapsingToolbarLayout 滚动 PreferenceFragment

objective-c - 如何提高滞后的 UIView 动画的性能

android - 使用 DialogFragment 和 ScrollView 时共享元素过渡闪烁

css - float 元素使动画平滑并反转方向?

jQuery - 事件/动画时间线

android - ActivityOptions.makeSceneTransitionAnimation 创建一个空 Activity

Android 共享元素从启动画面到主 Activity 的过渡