LiveData观察器发生更改而不是保持不变的问题 [英] Problem with LiveData observer changing rather than staying the same

查看:68
本文介绍了LiveData观察器发生更改而不是保持不变的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到LiveData的RecyclerView问题.每次更新时,RecyclerView内容都会一直返回到顶部.

I have a problems with a RecyclerView linked to LiveData. The RecyclerView content goes back up all the way to the top every time there is an update.

显然,问题出在我创建适配器的方式上,因为观察者每次都在变化.大概我明白了.那个观察者被卡在lambda里面,这是我仍然非常熟悉的许多事物之一.

Apparently the problem lies in the way I created the adapter, as the Observer changes every time. Or so I understood. That observer is stuck inside a lambda, which is one of the many things I am still very remotely familiar with.

这显然是问题所在:

languagesViewModel.getLanguagesForUser().observe ( viewLifecycleOwner, { languages ->
        LANGUAGES_SCREEN_VIEWS.languagesRecyclerView.adapter = LanguagesRecyclerViewAdapter ( languages ) } )

我试图从中取出点点滴滴,但那没用...

I tried to get bits out of it, but it didn't work...

我的第一次尝试是

var languageAdapter = LANGUAGES_SCREEN_VIEWS.languagesRecyclerView.adapter

languagesViewModel.getLanguagesForUser().observe( requireActivity(), { languages ->
            languageAdapter = LanguagesRecyclerViewAdapter(languages) } )

有警告,该变量已分配但从未访问过,并且语言列表为空.

There was a warning that the variable is assigned but never accessed, and the list of languages is empty.

我的第二次尝试是

    val languages = mutableListOf < LanguagesEntity > ()
    var languagesAdapter = LanguagesRecyclerViewAdapter(languages)

    languagesViewModel.getLanguagesForUser().observe( requireActivity(), { languages ->
        LANGUAGES_SCREEN_VIEWS.languagesRecyclerView.adapter = languagesAdapter } )

我希望很明显,很遗憾,我对我应该做的事情一无所知.并非缺乏尝试,因为我在寻求帮助之前在黑暗中刺了三天,显然这是代码的一部分,这就是问题所在.如果我也找到解决方案,我会尝试回答我自己的未解决问题.这次不会发生.

I hope it's clear that unfortunately I haven't got a clue of what I should really be doing. Not for lack of trying because I stabbed in the dark for 3 days before asking for help, and apparently that is the part of the code which is the problem. I do try to answer my own unanswered questions if I find a solution too. This time it just won't happen.

非常感谢您.

推荐答案

您应该在 adapter 中具有 internal 函数来设置值并通知适配器.而且,不要在 LiveData观察器中设置适配器.

you should have an internal function in adapter for setting value and notify the adapter. And also, DONT set your adapter in LiveData observer.

您应该考虑这种架构.

适配器

// STEP 1 - make a function to notify the variable
internal fun setLanguage(lang: List<LanguagesEntity>) {
       languages = lang
       notifyDataSetChanged()
 }

// STEP 2 - setup recyclerview before everything
val languages = mutableListOf < LanguagesEntity > ()
var languagesAdapter = LanguagesRecyclerViewAdapter(languages)
LANGUAGES_SCREEN_VIEWS.languagesRecyclerView.adapter = languagesAdapter 


// STEP 3 - set new value to adapter
languagesViewModel.getLanguagesForUser().observe( requireActivity(), { languages ->
    
    languagesAdapter.setLanguage(language)

} )

希望您很清楚.

这篇关于LiveData观察器发生更改而不是保持不变的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆