Android ListView Optimization Best Practices

Source: Internet
Author: User

Original Address link: Http://stackvoid.com/list-view-optimization-best-practice-android/


I have a blog that teaches you how to improve the situation with Convertview and Viewholder (static), but when the ListView loads a lot of complex layouts and pictures, even with Convertview and Viewholde R,listview or lag, this article discusses how to load complex list_item while ensuring the flow of the ListView.

The core idea is

Listen for the load, load data asynchronously.

The Main method:

  1. First judge the current ListView state, only the ListView stop sliding to open the new line loads load data, other states are ignored.

  2. Use the Getfirstvisibleposition and Getlastvisibleposition methods to display item.

  3. Time consuming tasks must not be done in the GetView method, preferably asynchronously.

The specific code is as follows:

1 //1. Judging the ListView State2 Abslistview.Onscrolllistener Onscrolllistener = New Abslistview.Onscrolllistener() {//ListView3 //Touch Events4 5  Public void onscroll(Abslistview View, int Firstvisibleitem, int VisibleItemCount, int Totalitemcount) {6 }7 8  Public void onscrollstatechanged(Abslistview View, int scrollstate) {9 Switch (scrollstate) {Ten    Case Abslistview.Onscrolllistener.scroll_state_fling://Sliding State One   Threadflag = false; A    Break; -    Case Abslistview.Onscrolllistener.Scroll_state_idle://Stop -   Threadflag = true; the   Startthread();//Open new thread, load data -    Break; -    Case Abslistview.Onscrolllistener.Scroll_state_touch_scroll://Touch ListView -   Threadflag = false; +    Break; -   default: +   //Toast.maketext (CONTEXTT, "Default", A   //Toast.length_short). Show (); at    Break; -   } - } - };

Believe to do the above three points, you can use the ListView freely, O (∩_∩) o haha ~

Android ListView Optimization Best Practices

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.