开发者

Destroying thread, during scrolling AbsListView

开发者 https://www.devze.com 2023-02-09 22:49 出处:网络
I want to solve next problem and hope for your help. I have got AbsListView with a set of images. Images are inflated by background thread which download it from the internet. I make this thread, beca

I want to solve next problem and hope for your help. I have got AbsListView with a set of images. Images are inflated by background thread which download it from the internet. I make this thread, because I don't want to impede the UI thread. So when user scrolls AbsListView I want to destroy the backgro开发者_C百科und thread and start it after stop scrolling.

To do it I need at least override onScroll method, but I don't know what should I write in it's body, because when I try to use Thread.destroy() method (which is deprecated) I got NoSuchMethodError.

When I define it as null if onScroll, It set the downloaded images into scrolling items.

Please, I need your advice how to cope with this situation.


It is generally not recommended to use threading capabilities by extending Thread. Android has a wonderful thing called AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

It's easy to use, and has a convenience methods, such as .cancel(); And if java api method is deprecated you shouldn't use it.


Please see the below article for a discussion of approaches to stopping a Thread in Java.

  • How to Stop a Thread or a Task

I'm not convinced that an AsyncTask would be a good fit for your situation, as the cancel() method does not actually stop the processing that is occurring in doInBackground(), it merely prevents onPostExecute() from being called and instead calls onCancelled().


dude check out the library that async downloads/cache images from the web https://github.com/koush/UrlImageViewHelper

one line of code.. simple as that =)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号