我如何使用MVVM进行回收科

发布于 2025-01-28 07:40:24 字数 1293 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛上了 2025-02-04 07:40:24

首先,不要在获取新数据时重新创建适配器。
只需将其与空甲供电在弹药上进行实例化。

typeRecy = itemView.findViewById(R.id.typeRecy)
        typeRecy.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        typeRecy.setHasFixedSize(true)
        val recyclerAdapter = recyclerAdapter(emptyList())
        typeRecy.adapter = recyclerAdapter
        typeVM = ViewModelProvider(this)[TypeViewModel::class.java]

其次,您不需要两个变量(键入和打字员),但其中只有一个将是mutableLiveData:

class TypeViewModel : ViewModel() {

    val typeCard = MutableLiveData<ArrayList<TypeCard>>()
    fun setList() {
        arrayOf(
            "Pizza" to R.drawable. group_1,
            "Tacos" to R.drawable. group_3,
            "Salads" to R.drawable. group_4,
            "Burgers" to R.drawable. group_2,
            "Soups" to R.drawable. group_5,
            "Drinks" to R.drawable. group_8
        ).map { (nameType, backgroundType) ->
            TypeCard(nameType, backgroundType)
        }.let(typeCard::postValue)
    }

}

最后,在片段中,您只需要观察typecepecepecerverivedata,并且在适配器中,有一种方法来更新列表和列表和列表和请勿每次调用setList。


typeRecy = itemView.findViewById(R.id.typeRecy)
        typeRecy.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        typeRecy.setHasFixedSize(true)
        val recyclerAdapter = recyclerAdapter(emptyList())
        typeRecy.adapter = recyclerAdapter
        typeVM = ViewModelProvider(this)[TypeViewModel::class.java]
        typeVM.setList()
        typeVM.typeCard.observe(viewLifecycleOwner, Observer {
            recyclerAdapter.updateList(it)
        })

Firstly, do not recreate your adapter each time you get new data.
Just instanciate it on the onCreate with an emptyList.

typeRecy = itemView.findViewById(R.id.typeRecy)
        typeRecy.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        typeRecy.setHasFixedSize(true)
        val recyclerAdapter = recyclerAdapter(emptyList())
        typeRecy.adapter = recyclerAdapter
        typeVM = ViewModelProvider(this)[TypeViewModel::class.java]

Secondly, you don't need two variable (typeCard and typeList) but only one of them that will be the MutableLiveData :

class TypeViewModel : ViewModel() {

    val typeCard = MutableLiveData<ArrayList<TypeCard>>()
    fun setList() {
        arrayOf(
            "Pizza" to R.drawable. group_1,
            "Tacos" to R.drawable. group_3,
            "Salads" to R.drawable. group_4,
            "Burgers" to R.drawable. group_2,
            "Soups" to R.drawable. group_5,
            "Drinks" to R.drawable. group_8
        ).map { (nameType, backgroundType) ->
            TypeCard(nameType, backgroundType)
        }.let(typeCard::postValue)
    }

}

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.


typeRecy = itemView.findViewById(R.id.typeRecy)
        typeRecy.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        typeRecy.setHasFixedSize(true)
        val recyclerAdapter = recyclerAdapter(emptyList())
        typeRecy.adapter = recyclerAdapter
        typeVM = ViewModelProvider(this)[TypeViewModel::class.java]
        typeVM.setList()
        typeVM.typeCard.observe(viewLifecycleOwner, Observer {
            recyclerAdapter.updateList(it)
        })

眼眸里的那抹悲凉 2025-02-04 07:40:24

从您提供的代码中,您的观察回调中看起来像 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

奈何桥上唱咆哮 2025-02-04 07:40:24

解决方案只是将此移出回调

typeVM.setList()

the solution was just to move this out of the callback

typeVM.setList()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文