ListView optimization Why Viewholder with Static class (reprint)

Source: Internet
Author: User

If someone doesn't understand why Viewholder can play an optimization role,
I'll make a brief statement here:
Android's Findviewbyid action is time-consuming and requires traversing the tree structure of the layout to find the corresponding view. So if we want to optimize this piece, we need to cache the content after find, so we use the Viewholder method.

This method is easy to understand, but maybe some people are more serious, like me, this can play a role in caching, then why do internal classes have to be declared static, ordinary internal classes do not also play such an effect? This question also bothered me for a long time. Until I read "effective Java" this book, only to solve my confusion, originally this is not Android optimization, but Java advocated optimization.

"Effective Java" 22nd a static member class is preferred
One of the suggestions is:
If the declaring member class does not require access to the perimeter instance, always place the static modifier in its declaration, making it a static member class, not a non-static member class.
Because an instance of a non-static member class contains an additional reference to a perimeter object, saving the reference consumes time and space and causes the perimeter class instance to be persisted when it is eligible for garbage collection. If you need to assign an instance without a perimeter instance, you cannot use a non-static member class because an instance of a non-static member class must have a perimeter instance.

PS: Highly recommend "effective Java" this book, will let you have unexpected harvest.



Wen/cppalien (author of Jane's book)
Original link: Http://www.jianshu.com/p/aa965968cbf5
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

ListView optimization Why Viewholder with Static class (reprint)

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.