Grid Recyclerview 上共享元素转换的反向动画总是移动到最后一个项目

Reverse animation for Shared Element transition on Grid Recyclerview always moves to last item

我在单击 recyclerview 项目时使用共享元素转换。单击项目时,imageView 平滑地过渡到细节 activity。但是,在单击后退按钮时,imageView 的转换总是转到 recyclerView 的最后一项。 我无法理解其背后的原因,欢迎任何意见。

这是我的代码。

<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowContentTransitions">true</item> </style>

gridItem.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?attr/selectableItemBackground">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recyclerItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/spacing_xsmall">

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/categoryImage"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_gravity="center"
            android:background="@color/colorPrimary"
            android:scaleType="fitXY"
            android:transitionName="AnimateTitle"
            />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:padding="@dimen/spacing_large"
            android:textColor="@color/colorAccent"
            android:textStyle="bold"/>
    </LinearLayout>
</FrameLayout>

DetailActivity.xml

  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.thinkinghats.isittrue.GameActivity"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="56dp"
        android:layout_width="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp">

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/categoryImage"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:scaleType="fitXY"
            android:transitionName="AnimateTitle"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/title"
            android:textSize="25sp"
            android:textStyle="bold"
            android:textColor="@color/colorAccent"
            />
    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/tools"
        android:id="@+id/card_question"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:layout_below="@id/toolbar"
        android:layout_margin="@dimen/spacing_xlarge"
        android:background="@color/white"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintVertical_bias="0.502"
        card_view:cardCornerRadius="4dp">
    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

And this is how i call the transition from MainActivity

public void onItemClick(View view, ViewModel viewModel) {
        Intent startIntent = new Intent(this, DetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("categoryTitle", viewModel.getText());
        bundle.putInt("categoryImage", viewModel.getImage());
        startIntent.putExtras(bundle);          
        startActivity(startIntent,ActivityOptions.makeSceneTransitionAnimation(this,view.findViewById(R.id.categoryImage),"AnimateTitle").toBundle());     
    }

事实证明,我在尝试某些操作时为 recyclerView onEnterAnimationComplete() 设置了适配器。在 onCreate() 事件上设置适配器后,问题就解决了。

我猜测,由于适配器曾经在 onEnterAnimationComplete() 上重置,因此无法让过渡知道哪个项目启动了它。