Giter Club home page Giter Club logo

imagelistview's Introduction

ImageListview

实现了异步加载图片,并且在滑动停止后再加载新图片,提高效率。

总结问题,主要有一下几点:

  1. 只使用简单的convertView与viewHolder来提高效率时,当从网络加载图片时速度较慢,并且由于listview重用view的机制,快速滑动时会出现图片乱序的状况,这种现象原因时,对于每个图片开启一个线程请求图片,可能在请求图片返回之前,由于滑动,本view已被其他item重用,载入了其对应的图片,而此时之前一个请求的图片正好返回,则会覆盖已经正确的图片,导致顺序错乱。 解决方法:加入一个简单的校验,即给每个imageview使用url设定一个tag,当从网络获取图片返回时,在将图片赋值给imageview之前,首先检测一下该imageview的tag,确认当前的imageview是否需要本次获取到的图片,如果相同才接受。这样可以解决之前请求的图片导致的图片乱序问题。

  2. 使用之前的方法后,图片仍会出现闪动情况,即本来item是第6项,但对应的图片可能是12,之后过一会才会变成6。这也是缓存机制引起的问题,当一个view被重用时,它仍显示之前时的图片,直到该item的图片从网络获取过来。为此,可以在view重用的时候,先用默认图片覆盖一下,这样看起来不会出现错乱的感觉。

  3. 至于为什么文字没有错乱而图片出现了错乱,主要是因为文字是从本地加载的,速度很快,在看到view出现之前的一瞬间,就已经完成了重用view的文字更新。并且发现从本地加载图片时,速度也是很快,不会出现乱序。所以采用LRUCache来缓存从网络请求来的图片,这样既可以减少图片的请求数量,每个图片只请求一次,减少流量消耗,并且对于已经获取过得图片,直接从缓存中直接加载,速度快,就会和文字一样,不会出现乱序了。

  4. 为了进一步优化,可以在listview滚动时并不联网获取图片,而只是当listview停下来后,获取当前显示的这几张图片,这样可以调滑动时的顺滑度。实际操作时遇到了需要注意的地方,虽然联网获取图片只在listview停止后进行,但是对于已经获取到的图片,则不用遵守这个规矩,直接加载即可。否则的话会出现,虽然之前的图片已经加载过,但是滚动回去时,并没有显示图片,而当滚动停止后,才一下子显示出所有图片。

原文

imagelistview's People

Contributors

lqcice avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.