Android ApiDemos example resolution (180): Views-& gt; Lists-& gt; 13. Sl

Source: Internet
Author: User

For a long list, if it is time-consuming to load each list item (such as downloading images from the Network), if you need to quickly scroll the list, you can use an alternative name or other items that can be quickly loaded to the list item first. When the list stops scrolling, the actual list items are loaded, which greatly improves the LIST Response performance.

In this example, a string array is displayed in ListView to simulate loading of a time-consuming list item (although the display text list item is actually very fast). When the list starts to scroll or fly fast, use a temporary data to bind list items (such as Loading ...), When the list stops scrolling, use the text to be displayed to replace the previously displayed temporary data.

Let's take a look at the getView Implementation of the custom SlowAdapter:


[Java]
Public View getView (int position,
View convertView, ViewGroup parent ){
TextView text;
 
If (convertView = null ){
Text = (TextView) mInflater. inflate
(Android. R. layout. simple_list_item_1,
Parent, false );
} Else {
Text = (TextView) convertView;
}
 
If (! MBusy ){
Text. setText (mStrings [position]);
// Null tag means the view has the c
// Orrect data
Text. setTag (null );
} Else {
Text. setText ("Loading ...");
// Non-null tag means the view still
// Needs to load it's data
Text. setTag (this );
}
 
Return text;
}

Public View getView (int position,
View convertView, ViewGroup parent ){
TextView text;

If (convertView = null ){
Text = (TextView) mInflater. inflate
(Android. R. layout. simple_list_item_1,
Parent, false );
} Else {
Text = (TextView) convertView;
}

If (! MBusy ){
Text. setText (mStrings [position]);
// Null tag means the view has the c
// Orrect data
Text. setTag (null );
} Else {
Text. setText ("Loading ...");
// Non-null tag means the view still
// Needs to load it's data
Text. setTag (this );
}

Return text;
}

You can see that in ListView busy (scrolling), set the text of textview to temporary data (loading ...") And set the textView tag to non-empty. The TextView Tag can use any object. In this example, it is used as a tag. If it is not empty, it indicates that the current TextView uses temporary data. When ListView is not busy (stop scrolling), set TextView to the text to be displayed (or other time-consuming operations), and set its Tag to null, textView displays real data.

Add rolling event processing for ListView


[Java]
GetListView (). setOnScrollListener (this );}
...
 
Public void onScrollStateChanged (AbsListView view,
Int scrollState ){
Switch (scrollState ){
Case OnScrollListener. SCROLL_STATE_IDLE:
MBusy = false;
 
Int first = view. getFirstVisiblePosition ();
Int count = view. getChildCount ();
For (int I = 0; I <count; I ++ ){
TextView t = (TextView) view. getChildAt (I );
If (t. getTag ()! = Null ){
T. setText (mStrings [first + I]);
T. setTag (null );
}
}
 
MStatus. setText ("Idle ");
Break;
Case OnScrollListener. SCROLL_STATE_TOUCH_SCROLL:
MBusy = true;
MStatus. setText ("Touch scroll ");
Break;
Case OnScrollListener. SCROLL_STATE_FLING:
MBusy = true;
MStatus. setText ("Fling ");
Break;
}
}

GetListView (). setOnScrollListener (this );}
...

Public void onScrollStateChanged (AbsListView view,
Int scrollState ){
Switch (scrollState ){
Case OnScrollListener. SCROLL_STATE_IDLE:
MBusy = false;

Int first = view. getFirstVisiblePosition ();
Int count = view. getChildCount ();
For (int I = 0; I <count; I ++ ){
TextView t = (TextView) view. getChildAt (I );
If (t. getTag ()! = Null ){
T. setText (mStrings [first + I]);
T. setTag (null );
}
}

MStatus. setText ("Idle ");
Break;
Case OnScrollListener. SCROLL_STATE_TOUCH_SCROLL:
MBusy = true;
MStatus. setText ("Touch scroll ");
Break;
Case OnScrollListener. SCROLL_STATE_FLING:
MBusy = true;
MStatus. setText ("Fling ");
Break;
}
}

Set busy to true when ListView is rolling or moving fast. When the rolling is stopped, SCROLL_STATE_IDLE and busy is false. Then, determine whether to display the actual text string based on the Tag value of TextView.

At the bottom of this example, a text box is used to display the scrolling status of the current ListView.

 

 

 


 

 

 


 

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.