我如何使用MVVM进行回收科
我正在尝试使用MVVM进行回收瓶 请有任何帮助! KT文件:
typeRecy = itemView.findViewById(R.id.typeRecy)
typeRecy.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
typeRecy.setHasFixedSize(true)
typeVM = ViewModelProvider(this)[TypeViewModel::class.java]
typeVM.typeCard.observe(viewLifecycleOwner, Observer {
typeVM.setList()
typeRecy.adapter = recyclerAdapter(it)
})
ViewMoedel:
class TypeViewModel: ViewModel() {
//
private lateinit var typeList: ArrayList<TypeCard>
lateinit var bgType: Array<Int>
lateinit var nameType: Array<String>
var typeCard = MutableLiveData<ArrayList<TypeCard>>()
fun setList(){
bgType= arrayOf(R.drawable.group_1,R.drawable.group_3,R.drawable.group_4,R.drawable.group_2,R.drawable.group_5,R.drawable.group_8)
nameType= arrayOf("Pizza", "Tacos", "Salads","Burgers","Soups" , "Drinks")
typeList = arrayListOf()
putTypeData()
//
}
fun putTypeData() {
for(i in nameType.indices){
val type = TypeCard(nameType[i], bgType[i])
typeList.add(type)
}
typeCard.value = typeList
}
}
I am trying to do a recyclerView with mvvm, but I get empty recyclerView as a result
any help please!
the kt file :
typeRecy = itemView.findViewById(R.id.typeRecy)
typeRecy.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
typeRecy.setHasFixedSize(true)
typeVM = ViewModelProvider(this)[TypeViewModel::class.java]
typeVM.typeCard.observe(viewLifecycleOwner, Observer {
typeVM.setList()
typeRecy.adapter = recyclerAdapter(it)
})
the viewMoedel :
class TypeViewModel: ViewModel() {
//
private lateinit var typeList: ArrayList<TypeCard>
lateinit var bgType: Array<Int>
lateinit var nameType: Array<String>
var typeCard = MutableLiveData<ArrayList<TypeCard>>()
fun setList(){
bgType= arrayOf(R.drawable.group_1,R.drawable.group_3,R.drawable.group_4,R.drawable.group_2,R.drawable.group_5,R.drawable.group_8)
nameType= arrayOf("Pizza", "Tacos", "Salads","Burgers","Soups" , "Drinks")
typeList = arrayListOf()
putTypeData()
//
}
fun putTypeData() {
for(i in nameType.indices){
val type = TypeCard(nameType[i], bgType[i])
typeList.add(type)
}
typeCard.value = typeList
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,不要在获取新数据时重新创建适配器。
只需将其与空甲供电在弹药上进行实例化。
其次,您不需要两个变量(键入和打字员),但其中只有一个将是mutableLiveData:
最后,在片段中,您只需要观察typecepecepecerverivedata,并且在适配器中,有一种方法来更新列表和列表和列表和请勿每次调用setList。
Firstly, do not recreate your adapter each time you get new data.
Just instanciate it on the onCreate with an emptyList.
Secondly, you don't need two variable (typeCard and typeList) but only one of them that will be the MutableLiveData :
Finally, in the fragment you just need to observe the typeCardLiveData and in your adapter, have a method to update the list and DO NOT call setList each time.
从您提供的代码中,您的观察回调中看起来像 typevm.setlist()从未被调用,因为在livedata typecard中没有数据首先可以观察到。
尝试将 typevm.setlist()移出观察者。
同样,在观察 typecard的数据的数据时,最好将适配器设置为观察者外部的recylerview,并且仅更新适配器的数据
From your provided code it looks like typeVM.setList() in your observe callback is never called, since there is no data in the LiveData typeCard to be observed in the first place.
Try moving typeVM.setList() out of the Observer.
Also it would be good to set the adapter to the RecylerView outside of the Observer and only update the data of the adapter, when observing the data of typeCard
解决方案只是将此移出回调
the solution was just to move this out of the callback