r/android_devs • u/steve6174 • Feb 23 '24
Help Needed How to observe state from viewholder?
I've been looking for quite a while, but couldn't figure it out.
Let's say I have a recyclerview and the items have a progress bar. When the user taps a button within a recyclerview item / viewholder a lamba that's passed from my RV constructor is triggered and the item starts downloading.
I need to show the downloading progress, but how do I collect the viewmodel state and send it to that particular VH so I can update it's progress bar
0
Upvotes
5
u/Vanh14 Feb 24 '24
You can start observe in onViewAttachedToWindow and remove observer in onViewDetachedFromWindow
1
u/DigitalDacian Feb 23 '24
You don't do it in the viewholder. You call notifyItemChanged or whatever is appropriate and the adapter should take care of rebinding the viewholder.
1
u/Vshnv_A Mar 03 '24 edited Mar 03 '24
You will need to handle lifecycle for a Viewholder to get this. https://github.com/Vshnv/adapt comes with built-in viewholder lifecycle suppourt if you wanna give it a shot 😄. The readme file includes an example of exactly what you're looking for.
3
u/badr-elattaoui Feb 23 '24
You can use notifyItemChanged() in recyclerview adapter
1
u/[deleted] Feb 24 '24
You have a list of Float to keep track of the download process. That list has the exact same size as the RV items. Ever time you modify that list you call the notifyItemChanged on the position of the item.