Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

android - Problem with LiveData observer changing rather than staying the same

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.

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.

This is where the problem lies, apparently:

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...

My first attempt was this:

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.

My second attempt was this:

    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.

Thank you very much in advance.

question from:https://stackoverflow.com/questions/66056506/problem-with-livedata-observer-changing-rather-than-staying-the-same

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

You should consider this architecture.

Adapter

// 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)

} )

Hope it is clear to you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...