首页 > 解决方案 > 找到所需的转换:MaterialContainerTransform?

问题描述

在我的活动中,我正在尝试使用 MaterialContainerTransform 但它显示一个错误

找到所需的转换MaterialContainerTransform

override fun onCreate(savedInstanceState: Bundle?) {
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
    setEnterSharedElementCallback(MaterialContainerTransformSharedElementCallback())
    //this is giving error
    window.sharedElementEnterTransition = MaterialContainerTransform().apply {
        addTarget(android.R.id.content)
        duration = 300L
    }
    window.sharedElementReturnTransition = MaterialContainerTransform().apply {
        addTarget(android.R.id.content)
        duration = 250L
    }

我的依赖

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//material
implementation 'com.google.android.material:material:1.3.0-alpha02'

def coroutines_version = "1.3.8"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0'

}

标签: androidandroid-activitymaterial-designandroid-transitionsshared-element-transition

解决方案


有两种使用 AndroidX 转换MaterialContainerTransform的类型com.google.android.material.transition.MaterialContainerTransform ,包括适用于所有 API 级别的错误修复和com.google.android.material.transition。平台.MaterialContainerTransform类是建立在框架Transition 类之上的。

只有框架转换版本​​可用于窗口转换,因此您应确保导入正确的版本MaterialContainerTransform

import com.google.android.material.transition.platform.MaterialContainerTransform

推荐阅读