Android ListView Complex List Optimization Practice

Source: Internet
Author: User

Original: Android ListView Complex List Optimization Practice


Many social apps will inevitably involve complex list element implementations, a list of potentially large numbers of images, an indefinite list of comments, and a number of challenges for mobile programmers. This article is in the case of the implementation of a complex list of sliding, the use of a known optimization method under the guidance of an optimization practice, designed to improve the sliding flow of the ListView, the user to bring a good experience.

1: Design Draft:

This is the list of possible itemview, there are two, but there are many of the same places, such as a bit like the picture, comments and so on ... Among them, the number of comments and likes is variable .

2: Problems with General layout?

Avatar table: If you use your own layout LinearLayout and ImageView to complete, it will cause the layout to be deep, and a lot of addview operations will cause the list to slip.

Comment List: As with the Avatar table, the number of item will change, with the same problem as the Avatar table.

A large number of picture loading: it is not difficult to see the number of pictures is considerable, because each request back content may have many, if the data comes back on the request inside with all the picture link, even if the use of the thread pool, it will take up the CPU for a long period of time, it will also bring the interface is not fluent problem.

3: Guide the Optimization ideas:

(1): Viewholder mode, Reuse view and reduce child view find time, I believe we are not unfamiliar with this.

(2): Minimize layout levels

(3): Refresh only the changed part of view

(4): Avoid calling a method like AddView

(5): Load only the image required by the current view, and in the sliding list, stop the loading thread in the background, the UI thread to empty the CPU resources, at the time of the stop request.

(6): First load picture on processing (fillet/zoom etc.) and cached locally

4: Practice Process: 4.1: Layout and View section:

The "Avatar table" and "Comment list" as a whole of the custom view to achieve, the card structure on the right is basically a relativelayout can be achieved, so that the entire item can be basically controlled at four or three levels of depth above.

The "Avatar table" Implementation notes:

1): First should be filled with the default avatar, and then return to the picture should not be rude to use invalidate (), but the use of invalidate (rect rect), local refresh, in response to the Click event Replacement background.

2): Because the process of onmesure and OnDraw is more frequent and costly, the object should be reused as much as possible, such as (Paint, Rect ...).

"Comment List" Implementation highlights:

1): In addition to the above mentioned, here is a difficult point is the drawing of the string, when should be changed line? Here involves very complex word processing, especially different countries of different text systems, fortunately, these in the Android API has a related implementation, the author is found in the TextView, that is, android.text.Layout of several implementation classes , the author here just uses the dynamiclayout to realize. With this class you can get the height information of the string (onmeasure), the number of rows, etc... And it has achieved the draw process, just adjust the position of the canvas (canvas.translate), directly call on it, very convenient.

"Extra Elements"

1): Back to the design, we see the possibility of stock information, for this extra element,<viewstub> most suitable ...

Data changes for "ListView":

1): notifydatachanged () can be called for the entire item increment or delete action.

2): For data changes within the item, do not use notifydatachanged () rudely, but change the view that produces the change.

4.2: Picture Processing Section:

The idea at the beginning of the writer is to do:

1): Thread pool, and can pause all download threads and resume Downloads

2): Dual cache, local persistent cache, and memory level cache

3): Swipe to pause all downloads and assign CPU resources to the UI thread.

In the process of implementation, in multithreading here often do bad, often problem, very fortunate to find an open source project on GitHub can perfectly solve all the problems above. Now our company's Android project all picture loading has been proudly used on the ... Attached address:Android-universal-image-loader

5: The effect after adjustment:

Because the author does not retain the previous interface when the code, so can only demonstrate the final implementation of the results, can not be compared before and after. The phone is Xiaomi 4, the chart is the result of the developer option in the profile GPU rendering, which is constantly sliding before and after opening. Here you can see that most of the situation is below the green line, indicating that the overall experience is still very smooth.

Like the stocks of friends can also go further to download our application: Micro-wealth, will be in the 3.0 version can experience.


Android ListView Complex List Optimization Practice

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.