AsyncTask loading image RecyclerView

孤人 提交于 2019-12-04 11:13:53

As the name "RecyclerView" indicates, it recycles/reuses the views created to display your items/cards.

So, lets assume your RecyclerView has 3 CardViews which it recycles as you scroll, and we have 4 items to display the contents of.

Initially content of item 1 gets displayed in CardView 1, item 2 gets displayed in CardView 2 and item 3 gets displayed in CardView 3.

Now, as you scroll, CardView 1 disappears, gets recycled and contents of item 4 are shown in CardView 1.

As long as you don't reset the before inserted contents, CardView 1 will display them - in your case - as long as the AsyncTask needs to complete the contents of the before set item will be displayed.

To solve your problem you may want to use an imageloading (and caching) library like:

Your solution is also prone to race conditions (when later tasks complete before earlier ones)

I think you should use a third party library like Picasso to asynchronously download images from a url. The library provides much more than that, and you can focus more on the functionality of your app rather than get tangled in setting up your own AsynkTask.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!