首页 > 解决方案 > 活动转换时共享元素出现在所有视图的前面

问题描述

大家好,我对共享元素没什么问题。

我在活动转换时使用了共享元素。

当单击 recyclerView 项目时,该视图位于所有视图的前面。

正如我点击到最下面的图片时所看到的

在此处输入图像描述

开始转换片段 --> 活动

private fun itemClicked(pos: Int, view: View) {
        val intent = Intent(context, DetailActivity::class.java)
            val title = view.findViewById<TextView>(R.id.title)
            val photo = view.findViewById<ImageView>(R.id.photo)
            intent.putExtra(IMAGE_TRANSITION_NAME, photo.transitionName)
            intent.putExtra(TITLE_TRANSITION_NAME, title.transitionName)
            val pair1 =
                Pair.create(
                    photo as View,
                    ViewCompat.getTransitionName(photo).toString()
                )
            val pair2 =
                Pair.create(
                    title as View,
                    ViewCompat.getTransitionName(title).toString()
                )
            val options = activity?.let {
                ActivityOptionsCompat.makeSceneTransitionAnimation(it, pair2, pair1)
            }
            startActivity(intent, options?.toBundle())

    }

我的自定义视图持有人

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    android:padding="0dp"
    app:contentPaddingBottom="0dp"
    app:contentPaddingLeft="0dp"
    app:contentPaddingRight="0dp"
    app:contentPaddingTop="0dp"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

       <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:maxLines="2"
            app:autoSizeMaxTextSize="@dimen/primary_text_medium"
            app:autoSizeMinTextSize="@dimen/primary_text_small"
            app:autoSizeTextType="uniform"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/photo"
            tools:text="Deniz subaşı" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

标签: androidandroid-layoutandroid-intentshared-element-transitionactivity-transition

解决方案


推荐阅读