gpt4 book ai didi

android - 为什么 LiveData 只影响 Android Studio 中的主布局而不影响子布局 UI?

转载 作者:行者123 更新时间:2023-12-02 12:44:12 25 4
gpt4 key购买 nike

FragmentHome加载 layout_home.xml,layout_home.xml 显示 recyclerview和一个名为 btnMain 的按钮recyclerview包括项目布局 layout_voice_item.xml,它显示一个名为 btnChild 的按钮。
我用 displayCheckBox : LiveData<Boolean>使用代码 android:visibility="@{!aHomeViewModel.displayCheckBox? View.VISIBLE: View.GONE}" 控制是否同时显示 btnMain 和 btnChild .
我找到 btnMain更改displayCheckBox的值可以显示或不显示,但btnChild一直显示,为什么?
FragmentHome.kt

class FragmentHome : Fragment() {

private lateinit var binding: LayoutHomeBinding

private val mHomeViewModel by lazy {
getViewModel {
HomeViewModel(provideRepository(mContext))
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(
inflater, R.layout.layout_home, container, false
)
binding.lifecycleOwner = this.viewLifecycleOwner
binding.aHomeViewModel=mHomeViewModel

val adapter = VoiceAdapters(mHomeViewModel)
binding.mvoiceList.adapter=adapter
mHomeViewModel.listVoiceBySort.observe(viewLifecycleOwner){
adapter.submitList(it)
}
...
return binding.root
}
}
HomeViewModel.kt
class HomeViewModel(private val mDBVoiceRepository: DBVoiceRepository) : ViewModel() {

private val _displayCheckBox = MutableLiveData<Boolean>(true)
val displayCheckBox : LiveData<Boolean> = _displayCheckBox

fun setCheckBox(isDisplay:Boolean){
_displayCheckBox.value = isDisplay
}

...
}
语音适配器.kt
class VoiceAdapters (private val aHomeViewModel: HomeViewModel):
ListAdapter<MVoice, VoiceAdapters.VoiceViewHolder>(MVoiceDiffCallback()) {
...
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VoiceViewHolder {
return VoiceViewHolder(
LayoutVoiceItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}

override fun onBindViewHolder(holder: VoiceViewHolder, position: Int) {
val aMVoice = getItem(position)
holder.bind(aHomeViewModel, aMVoice)
}

inner class VoiceViewHolder (private val binding: LayoutVoiceItemBinding):
RecyclerView.ViewHolder(binding.root) {

fun bind(mHomeViewModel: HomeViewModel, aMVoice: MVoice) {
binding.aHomeViewModel = mHomeViewModel
binding.executePendingBindings()
}

}

...
}
class MVoiceDiffCallback : DiffUtil.ItemCallback<MVoice>() {
...
}
layout_home.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="aHomeViewModel"
type="info.dodata.voicerecorder.viewcontrol.HomeViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mvoice_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/layout_voice_item"
/>

<Button
android:id="@+id/btnMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{!aHomeViewModel.displayCheckBox? View.VISIBLE: View.GONE}"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
layout_voice_item.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/btnChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="@{!aHomeViewModel.displayCheckBox? View.VISIBLE: View.GONE}"
/>

</LinearLayout>
</layout>

最佳答案

LifecycleOwnerLayoutVoiceItemBinding可能会有所帮助。voiceViewHolder.binding.lifecycleOwner = yourLifecycleOwner

关于android - 为什么 LiveData 只影响 Android Studio 中的主布局而不影响子布局 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63825445/

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