gpt4 book ai didi

android - 使用底部导航 android kotlin 在 fragment 中实现 MVVM 的最佳实践

转载 作者:行者123 更新时间:2023-12-04 07:22:44 24 4
gpt4 key购买 nike

我正在使用带有 firebase 的底部导航在 fragment 中实现 MVVM。但这在我的情况下不起作用。我搜索了很多解决方案,但没有解决我的问题。
我在 fragment 中实现了 viewModel 并将观察者应用于它。在 ViewModel 类中,从存储库调用返回类型为 LiveData 的方法(从 firebase 获取数据)。
我正在使用这些依赖项
依赖

//    Fragment Navigation using JetPack
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
存储库.kt

// Get emergency requests
fun getEmergencyRequests(): LiveData<MutableList<EmergencyRequests>> {
val mutableData = MutableLiveData<MutableList<EmergencyRequests>>()

val requestListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists() && dataSnapshot.hasChildren()) {

// Get Post object and use the values to update the UI
val listData = mutableListOf<EmergencyRequests>()

for (snapshot in dataSnapshot.children) {
val model = snapshot.getValue(EmergencyRequests::class.java)

model?.let {
model.requestId = snapshot.ref.key.toString()
listData.add(it)
}
}

listData.sortByDescending { it.timestamp }
mutableData.value = listData


}
}

override fun onCancelled(error: DatabaseError) {
Log.e("EMERGENCIES_FAIL", error.message)
}

}

emergencyRequestRef?.addValueEventListener(requestListener)

return mutableData
}
View 模型.kt
private val repository: HomeRepository = HomeRepository()

fun getRequests(): LiveData<MutableList<EmergencyRequests>> {
repository.getEmergencyRequests()
val mutableData = MutableLiveData<MutableList<EmergencyRequests>>()
repository.getEmergencyRequests().observeForever {
mutableData.value = it
}
return mutableData
}
fragment .kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// Initialize
initialization()

// Recyclerview
binding.recyclerView.layoutManager = LinearLayoutManager(mContext)
adapter = EmergenciesAdapter(mContext, object : OnRequestClick {
override fun onClick(model: EmergencyRequests) {

}
})
binding.recyclerView.adapter = adapter

// Get emergencies
// getEmergencies()
viewModel = ViewModelProvider(mContext).get(HomeViewModel::class.java)


observer()

}

private fun observer() {
viewModel.getRequests().observe(viewLifecycleOwner, {
adapter.setDataList(it)
adapter.notifyDataSetChanged()
})
}

最佳答案

不确定这是否会为您解决,但如果您使用的是 bottom navigation view并想要 fragments维护backstack对于访问过的目的地,然后使用此版本的 Navigation dependency您可以保存backstacks自动,使用这些 Navigation dependencies :

implementation 'androidx.navigation:navigation-fragment-ktx:2.4.0-alpha01'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.0-alpha01'
更新
在您的存储库中,您能否取回数据?因为该函数可能会在从 firebase 获取数据之前返回。我建议像这样重构它:

Repository.kt

private val _data: MutableLiveData<MutableList<EmergencyRequests>> = MutableLiveData()

val data: LiveData<MutableList<EmergencyRequests>> get() = _data

// Get emergency requests
fun getEmergencyRequests() {

val requestListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists() && dataSnapshot.hasChildren()) {

// Get Post object and use the values to update the UI
val listData = mutableListOf<EmergencyRequests>()

for (snapshot in dataSnapshot.children) {
val model = snapshot.getValue(EmergencyRequests::class.java)

model?.let {
model.requestId = snapshot.ref.key.toString()
listData.add(it)
}
}

listData.sortByDescending { it.timestamp }
_data.value = listData


}
}

override fun onCancelled(error: DatabaseError) {
Log.e("EMERGENCIES_FAIL", error.message)
}

}

emergencyRequestRef?.addValueEventListener(requestListener)
}

ViewModel.kt

private val repository: HomeRepository = HomeRepository()

fun getRequests(): LiveData<MutableList<EmergencyRequests>> {
repository.getEmergencyRequests()

return repository.data
}

关于android - 使用底部导航 android kotlin 在 fragment 中实现 MVVM 的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68395330/

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