Explanation of Android open-source code ononscrolllistener: data is not loaded when ListView is rolled.

Source: Internet
Author: User

When using ListView, if the scrolling data loading operation is time-consuming, it is easy to get stuck on the screen during scrolling. One solution is not to load data during scrolling, instead, the data is loaded after the rolling stops. This also requires the OnScrollListener interface. For a brief description of this interface, see the previous article, where you can directly analyze the Code:
[Java]
Package hust. iprai. asce1885;
 
Import android. app. ListActivity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AbsListView;
Import android. widget. AbsListView. OnScrollListener;
Import android. widget. BaseAdapter;
Import android. widget. TextView;
 
Public class MainActivity extends ListActivity implements OnScrollListener {

Private TextView mStatus; // display the scrolling status
Private boolean mBusy = false; // identifies whether a scrolling operation exists.

/**
* Custom Adapter for display of views in ListView
*
*/
Private class SlowAdapter extends BaseAdapter {
Private LayoutInflater mInflater;

Public SlowAdapter (Context context ){
MInflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
}
 
/**
* The number of elements in the list depends on the number of data items.
*/
Public int getCount (){
Return mStrings. length;
}
 
/**
* Our simulated data is obtained from the array. Therefore, we can directly return the index value to obtain the corresponding data.
*/
Public Object getItem (int position ){
Return position;
}
 
/**
* Use the index of the array as the unique id
*/
Public long getItemId (int position ){
Return position;
}
 
/**
* Obtain the view of each row in the List.
*/
Public View getView (int position, View convertView, ViewGroup parent ){
TextView text;

// Assign a value to text
If (null = convertView ){
Text = (TextView) mInflater. inflate (android. R. layout. simple_list_item_1, parent, false );
} Else {
Text = (TextView) convertView;
}

If (! MBusy ){
// Data is not in the busy period of data loading (no scrolling ).
Text. setText (mStrings [position]);
// If tag is set to null, the view has the correct data.
Text. setTag (null );
} Else {
// The screen is currently in the scrolling stage. If no data is loaded, a prompt is displayed for loading data.
Text. setText ("Loading ...");
// If the tag is not empty, the view still needs to be loaded and displayed.
Text. setTag (this );
}

Return text;
}

}

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

MStatus = (TextView) findViewById (R. id. status );
MStatus. setText ("Idle ");

// Map data to TextView using a custom ListAdapter
SetListAdapter (new SlowAdapter (this ));

// Set the scroll listener
GetListView (). setOnScrollListener (this );
}
 
Public void onScroll (AbsListView view, int firstVisibleItem,
Int visibleItemCount, int totalItemCount ){

}
 
Public void onScrollStateChanged (AbsListView view, int scrollState ){
Switch (scrollState ){
Case OnScrollListener. SCROLL_STATE_IDLE: // Idle state for loading and displaying actual data
MBusy = false;

Int first = view. getFirstVisiblePosition ();
Int count = view. getChildCount ();
For (int I = 0; I <count; I ++ ){
TextView TV = (TextView) view. getChildAt (I );
If (TV. getTag ()! = Null) {// non-null indicates that data needs to be loaded.
TV. setText (mStrings [first + I]);
TV. 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;
Default:
MStatus. setText ("Are you kidding me! ");
Break;
}
}

Private String [] mStrings = {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam ",
"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis ",
"Afustm'l Pitu", "Airag", "Airedale", "Aisy cendh ",
"Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese ",
"Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh ",
"Anthoriro", "Appenzell", "Aragon", "Ardi Gasna", "Ardrahan ",
"Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago ",
"Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel ",
"Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal ",
"Banon", "Barry's Bay Cheddar", "Basing", "Basket Cheese ",
"Bath Cheese", "Bavarian Bergkase", "Baylough", "Beaufort ",
"Beauvoorde", "beenwhale Blue", "Beer Cheese", "Bel Paese ",
"Bergader", "Bergere Bleue", "berkader", "Beyaz Peynir ",
"Bierkase", "Bishop Kennedy", "Blarney", "Bleu d' Auvergne ",
"Bleu de Gex", "Bleu de Laqueuille", "Bleu de Septmoncel ",
"Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore ",
"Blue Vein (logical Alian)", "Blue Vein Cheeses", "Bocconcini ",
"Bocconcini (financialian)", "Boeren Leidenkaas", "Bonchester ",
"Bosworth "};


}
The layout file main. xml is as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">

<ListView android: id = "@ android: id/list"
Android: layout_width = "match_parent"
Android: layout_height = "0dip"
Android: layout_weight = "1"
Android: drawselectid Top = "false"/>

<TextView android: id = "@ + id/status"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: paddingLeft = "8dip"
Android: paddingRight = "8dip"/>

</LinearLayout>
Shows the program running result:



From ASCE1885

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.