Chooven
Chooven

Reputation: 105

Need to update an item in a recyclerview if it has been on screen for set amount of time

Using Kotlin, I have a recyclerview with an adapter that displays an Arraylist. What I need to do is set a Object.viewed property if it has been on screen for 2 seconds. Meaning if you can just scrolling quickly through the list nothing happens but if you stop and on one to read it then I need to make it as viewed, so I can increment a seen counter.

Thanks for any help or pointers.

Upvotes: 0

Views: 180

Answers (3)

Karma Maker
Karma Maker

Reputation: 104

Jeel Vankhede approach would work but slightly heavy for my taste. I would suggest to create a map ids to currentTime, add element there inside onViewAttachedToWindow and inside onViewDetachedFromWindow check if 2 seconds has passed. (Don't forget to remove id from map inside onViewDetachedFromWindow regardless)

Upvotes: 1

Onur Aydeniz
Onur Aydeniz

Reputation: 67

Try using countdowntimer in the OnResume function.

Object:Countdowntimer(2000,1000)........

Upvotes: 0

Jeel Vankhede
Jeel Vankhede

Reputation: 12138

You can take a handler with delay of 2 seconds & make your logic of Viewed in it. So, after 2 seconds handler will execute your code like below :

//Code inside bindViewHolder() method :
handler = new Handler(); // make it globally
handler.postDelayed(runnable, 2000); // make a runnable with your logic code of viewed.

Now override a method called onViewDetachedFromWindow(VH holder) and remove your handle callback here. so if your view get's detached before 2000 seconds, code won't be executed.

Upvotes: 1

Related Questions