I'm displaying a variable number of ViewHolders in a RecyclerView, each with a unique CountDownTimer in it, displayed in a TextView.
If I don't have a large number of ViewHolders in the RecyclerView then the CountDownTimers tick down as expected, second by second.
However, my issue arises when I have a large number (> 10 or so) of items in the RecyclerView. When I open the Activity with the RecyclerView, I see the CountDownTimers displayed to me functioning properly, however as I scroll up and down the entire RecyclerView, the CountDownTimers begin to malfunction.
By malfunction I mean that they fluctuate. For instance, one CountDownTimer will display 20:15 then jump to 30:16 then back to 20:14 then back to 30:15. It's very odd.
This is what I believe the issue is: as I scroll down the RecyclerView, ViewHolders with CountDownTimers disappear but their CountDownTimers are not destroyed, and when I scroll back to those ViewHolders there are multiple CountDownTimers being assigned to the TextViews which display their respective countdowns.
My question is this: Assuming what I described is indeed the issue, how do I cancel CountDownTimers in RecyclerView ViewHolders as they're scrolled past?
From My understanding about RecyclerView :
RecyclerView creates viewHolders as many as it needs to display plus one or more extra for smooth scrolling. When it needs to show new item it checks that any unused viewHolder existed. If existed then it reused the existing one otherwise creates new.
In your condition when you scroll very long then it tries to use existing viewHolder which might be started count down. Now when new item binds it starts another count down. Thats why it shows malfunction.
So, You can add a check & stop count down before starting a countdown. Hope it will help you.
Thanks in advance.