Full interpretation of ListView optimization and interpretation of ListView Optimization

Source: Internet
Author: User

Full interpretation of ListView optimization and interpretation of ListView Optimization
I. Reuse view II. Use viewholder

It is best to use static internal classes. Static internal class, do not hold external class reference, avoid Memory leakage...

As mentioned in objective java, the main function of static class is to reduce the memory overhead caused by a large number of internal classes holding external class references.

Non-static internal classes hold external class references, which involves releasing external classes dependent on internal classes. If the internal class references the external class too much, this will easily lead to the release of the external class.

3. Set listview height to match-parent 4. Use asynchronous loading when images are involved

In my project, each item in the listview has an image and many textviews, and all textviews must be set with non-system fonts. The Adapter uses ViewHolder optimization, the code in getView is very few, but it is still stuck. The data in my listview is a List of objects, and only the resource ID of the image to be displayed for the item is stored in the object, or the image path requires some operations to get the image, which is time-consuming. So I changed the original object, store the image objects directly in the objects corresponding to the item, and initialize these objects when the Adapter is initialized. Although the time required for listview display is a little longer, however, the result is that the listview slides smoothly a lot. Then I put the operation of obtaining the font TypeFace from assets in the Adapter initialization method, and saved the font through static variables, then, in getView, you only need to set the tapt for TextView. Face: You don't need to get the time it takes for the font from the asset. After the two steps above, my listview is not stuck at all, and it is very smooth.
The Optimization of the lateral navigation listview is to find the time-consuming operation in getView, and extract it, or use an Asynchronous Method to set data for the item layout. If synchronization is required, you can only prepare the data when the Adapter is initialized, and then bind it to getView.

5. Introduce thread pools and thread queues during asynchronous loading

In zookeeper Java, if a new thread is created every time a request arrives, the overhead is considerable. In actual use, the time and system resources consumed by the server that creates a new thread for each request on the creation and sale of the thread are as follows, it may even take much more time and resources than processing actual user requests. In addition to the overhead of creating and destroying threads, active threads also consume system resources. If too many threads are created in a JVM, the system resources may be insufficient due to excessive memory consumption or excessive switching. To prevent resource insufficiency, server applications need some methods to limit the number of requests processed at any given time point and minimize the number of threads created and destroyed, in particular, the creation and destruction of some threads that consume a large amount of resources should try to use existing objects for service. This is why the "pooled resources" Technology is created. The thread pool is mainly used to solve the thread lifecycle overhead and resource insufficiency issues. By reusing threads for multiple tasks, the overhead created by the thread is apportioned to multiple tasks, and because the thread already exists when the request arrives, this eliminates the latency caused by thread creation. In this way, the application can immediately respond to the request. In addition, adjust the number of threads in the thread pool appropriately to prevent resource insufficiency. Therefore, here we must maintain a local task queue, put all the images to be loaded into the queue, each time only load the current task, as the list slides, the current View is displayed, the task will be topped and loaded first. This not only avoids excessive threads occupying a large amount of resources, but also ensures the rational use of resources.

Vi. Multi-layout of listview 7. Paging loading of listview 8. Memory Optimization for images

When the program loads an image, the image size may be large, but when displayed on the mobile phone, the imageview will zoom out and display the image in the program, in fact, many details are not displayed in the image. However, when we use BitmapFactory to parse images, BitmapFactory will parse the complete image, create memory according to the image resolution, and convert it to Bitmap, which will occupy a very large amount of memory.

How to solve zookeeper?

IX. Introduction of three-level caching mechanism for image loading 10. Solution to the dislocation problem caused by asynchronous image loading 11. Stop loading images without moving images

Considering the actual human-computer interaction, asynchronous operations should not be triggered in each row of the ListView adapter. That is to say, when the ListView has a fling (fast sliding) operation, it makes no sense to start any asynchronous operation. Once the scroll stops or is about to stop, it is time to start displaying the content of each row.
When swiping the list quickly (SCROLL_STATE_FLING), the images in the item or the view that consumes resources can be retrieved but not displayed. The images are in the other two States (SCROLL_STATE_IDLE and SCROLL_STATE_TOUCH_SCROLL ), the view is displayed.

Specific implementation: http://blog.csdn.net/jdsjlzx/article/details/45914707

12. Reasons Why RecyclerView replaces listview

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.