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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…