Hi everyone! I was wondering if somebody could hel...
# android
p
Hi everyone! I was wondering if somebody could help me. I'm new on StateFlow and I found an issue developing my app: I have a layout with a RecyclerView, so whenever I use _state.value.copy( .. ) my Recycler refresh losing its position on scroll... I would like to keep that scroll position. How can I update my UiState without losing the scroll? 🙂
😶 2
a
Can you post your code!!
p
_state.value = _state.value.copy(events = events)
data class UiState(
val recyclerViewItems: List<MarvelItem>? = null
val events: List<EventItem>? = null
)
every time I update my _state.value with copy function my r_ecyclerViewItems_ reload and my recyclerView reload losing the scroll
k
How are you updating your RecyclerView data? I’m not as familiar with compose, but using XML there’s an issue where you have to specifically call
notifyDataSetChanged
, so that it would update. I think the root issue being that if you update the source information, it will clear the current rows, which put the index of the view to 0, then create all new ones with the new data. I could be wrong with compose, it may be different, but hopefully this at least helps a little.
j
Can you post the code where it observes the state and update the adapter with new
events
list?
p
Thanks guys: @Kevin S I'm using xml so, great! @jermainedilao the piece of code where I observe the UiState is in my fragment:
viewLifecycleOwner.launchAndCollect(viewModel.state) {
binding.recyclerViewItems = it.recyclerViewItems
binding.events = it.events
}
of course when I change events it would change recyclerViewItems, reloading everything. How can I update just events when I need (multiple times) and not recyclerViewItems (just in some occasions) or updating everything but maintaining scroll?
a
I think you have to use DiffUtill with RecyclerView so you can update only new changes not the whole list
plus1 2
p
mm interesting... I will research about that option, thanks!
a
Good Luck ✌️
❤️ 1
j
btw, I just remembered, I wrote an article before that might help you. https://jermainedilao.medium.com/demystifying-diffutil-itemcallback-class-8c0201cc69b1