【发布时间】:2020-03-23 12:42:42
【问题描述】:
我有一个搜索栏:
<SeekBar
android:id="@+id/sw_lock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="60dp"
android:max="100"
android:thumb="@drawable/ic_thumb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lbl_device_status" />
我正在使用数据绑定,一切正常。当我想更改拇指颜色时,问题就来了。
在一个片段中,我有一个包含 0...n 个视图的垂直线性布局,其中包含此搜索栏
response.observe(viewLifecycleOwner, Observer { list ->
activity?.run {
list.forEach { element ->
val mView = MyView(this)
mView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
mView.bind(element)
mView.didUnlock = { view, unlocked, element ->
//DO STUFF
}
binding.container.addView(mView)
}
}
})
这工作正常。我有 n 个实例,每个实例都能正常工作。
现在,我想在进度更改时更改拇指颜色,所以我有:
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
changeColor(binding.swLock.progress > 80)
}
而changeColor方法是这样的:
private fun changeColor(active: Boolean) {
val color = if(active) {
R.color.colorAccent
} else {
R.color.text_main
}
binding.swLock.progressDrawable.setTint(getColor(color))
binding.swLock.thumb.setTint(getColor(color))
}
奇怪的事情来了,progressDrawable 在每个实例中都发生了变化,拇指在所有实例中都发生了变化。我做错了什么?
感谢和问候
【问题讨论】: