我有一个layout_image
这样命名的 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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/grumpy_cat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我在几个活动/片段中包含 XML
<include
android:id="@+id/llytImage"
layout="@layout/layout_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
imageView
我通过使用 kotlinx 合成访问它在 Activity A 中设置了一个颜色过滤器:
import kotlinx.android.synthetic.main.layout_image.view.*
llytImage.imageView.setColorFilter(Color.RED)
它变成红色,我导航到活动 B。活动 B 还包含具有相同 ID 的相同 XML 布局。但是,imageView
在活动 B 中仍然是红色的。
似乎 Kotlin 缓存了状态imageView
并使用相同的视图来实现更好的性能。
但是,我不希望它缓存imageView
。我需要默认状态。我尝试了两种方法来阻止 Kotlin 缓存它:
@ContainerOptions(CacheImplementation.NO_CACHE)
在包含 XML 的活动中使用注释imageView.clearFindViewByIdCache()
导航到 Activity B 时调用
他们都没有为我工作。
如果您知道真正的解决方案,我将不胜感激。谢谢