gpt4 book ai didi

android - 当 LiveData 在 Android Studio 中的双向数据绑定(bind)中更改时,如何重置 LiveData

转载 作者:行者123 更新时间:2023-12-02 13:26:55 27 4
gpt4 key购买 nike

在 layout_detail.xml 中 EditText 使用双向数据绑定(bind)和 Button 使用单向数据绑定(bind)
我希望 Button 将在 aDetailViewModel.aMVoice.name 时启用被改变。
当 EditText 的内容发生变化时,aMVoice 的值在 DetailViewModel也将被更改,我想我可以重置 isChanged 的值,但我不知道怎么办?你可以告诉我吗?
layout_detail.xml

<layout 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">

<data>
<import type="android.view.View" />
<variable name="aDetailViewModel"
type="info.dodata.voicerecorder.viewcontrol.DetailViewModel" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:id="@+id/eTName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="@={aDetailViewModel.aMVoice.name}" />

<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="@{aDetailViewModel.isChanged}"
android:text="Save" />

</LinearLayout>

</layout>
代码
class DetailViewModel(private val mDBVoiceRepository: DBVoiceRepository, private val voiceId:Int) : ViewModel() {

val aMVoice=mDBVoiceRepository.getVoiceById(voiceId) //I hope to reset isChanged when aMVoice is changed

val isChanged: LiveData<Boolean> = MutableLiveData<Boolean>(false)
}


class DBVoiceRepository private constructor(private val mDBVoiceDao: DBVoiceDao){
fun getVoiceById(id:Int)=mDBVoiceDao.getVoiceById(id)
}

@Dao
interface DBVoiceDao{
@Query("SELECT * FROM voice_table where id=:id")
fun getVoiceById(id:Int):LiveData<MVoice>
}

@Entity(tableName = "voice_table", indices = [Index("createdDate")])
data class MVoice(
@PrimaryKey (autoGenerate = true) @ColumnInfo(name = "id") var id: Int = 0,
var name: String = "",
var path: String = "",
)

最佳答案

由于您想找出多个属性的值变化,我们应该将每个属性分隔在一个特定的 LiveData 中。通过 map转变。这些新的LiveData s 起到双向绑定(bind)的作用。
最后,要集成更改,最好的方法是使用 MediatorLiveData由每次更改触发,然后检查值。因此,从 DB 检查初始值的相等性就足够了。从 View 中接收到的值。

class DetailViewModel(...) {

private val aMVoice = mDBVoiceRepository.getVoiceById(voiceId)

val voiceName = aMVoice.map { it.name } as MutableLiveData<String>
val voicePath = aMVoice.map { it.path } as MutableLiveData<String>
// ... (similar for more attributes)

val isChanged = MediatorLiveData<Boolean>().apply {
addSource(voiceName) { postValue(currentMVoice != aMVoice.value) }
addSource(voicePath) { postValue(currentMVoice != aMVoice.value) }
// ... (similar for more attributes)
}

// it collects current values delivered from views as a `MVoice` object.
private val currentMVoice: MVoice?
get() = aMVoice.value?.copy(
name = voiceName.value ?: "",
path = voicePath.value ?: "",
// ... (similar for more attributes)
)
}
然后使用 voiceName , voicePath等在布局中:
<EditText
android:id="@+id/eTName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="@={aDetailViewModel.voiceName}" />

<EditText
android:id="@+id/eTPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:text="@={aDetailViewModel.voicePath}" />

// ...

使用 map改造别忘了加 lifecycle-livedata-ktx build.gradle 中的依赖项如果你以前没有。
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'

关于android - 当 LiveData<MVoice> 在 Android Studio 中的双向数据绑定(bind)中更改时,如何重置 LiveData<Boolean>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63432895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com