Package zy. lucifer. ListViewScroll;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. Gravity;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AbsListView;
Import android. widget. BaseAdapter;
Import android. widget. LinearLayout;
Import android. widget. ListView;
Import android. widget. ProgressBar;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. widget. AbsListView. OnScrollListener;
Import android. widget. LinearLayout. LayoutParams;
Public class ListViewScroll extends Activity {
/** Called when the activity is first created .*/
Private LayoutParams mLayoutParams = new LinearLayout. LayoutParams (
LinearLayout. LayoutParams. WRAP_CONTENT,
LinearLayout. LayoutParams. WRAP_CONTENT );
/**
* Set the target maximization attribute for the layout display.
*/
Private LayoutParams FFlayoutParams = new LinearLayout. LayoutParams (
LinearLayout. LayoutParams. FILL_PARENT,
LinearLayout. LayoutParams. FILL_PARENT );
ListView listView;
Private int lastItem = 0;
LinearLayout loadingLayout;
Private listViewAdapter adapter;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ListView = (ListView) findViewById (R. id. myList );
Log. I ("test", "onCreate (Bundle savedInstanceState) >>>>>>>>>>>>>> ");
// Linear Layout
LinearLayout layout = new LinearLayout (this );
// Set the horizontal layout direction
Layout. setOrientation (LinearLayout. HORIZONTAL );
// Progress bar
ProgressBar progressBar = new ProgressBar (this );
// Position of the progress bar
ProgressBar. setPadding (0, 0, 15, 0 );
// Add the progress bar to layout
Layout. addView (progressBar, mLayoutParams );
// Text content
TextView textView = new TextView (this );
TextView. setText ("loading ...");
TextView. setGravity (Gravity. CENTER_VERTICAL );
// Add the text to layout
Layout. addView (textView, FFlayoutParams );
// Set the gravity direction of layout, that is, the alignment mode is
Layout. setGravity (Gravity. CENTER );
// Set the footer layout of the ListView.
LoadingLayout = new LinearLayout (this );
LoadingLayout. addView (layout, mLayoutParams );
LoadingLayout. setGravity (Gravity. CENTER );
ListView. addFooterView (loadingLayout );
Adapter = new listViewAdapter ();
ListView. setAdapter (adapter );
ListView. setOnScrollListener (new OnScrollListener (){
@ Override
Public void onScroll (AbsListView view, int firstVisibleItem,
Int visibleItemCount, int totalItemCount ){
// TODO Auto-generated method stub
Log. I ("test", "Scroll >>> first:" + firstVisibleItem + ", visible:" + visibleItemCount + ", total:" + totalItemCount );
LastItem = firstVisibleItem + visibleItemCount-1;
Log. I ("test", "Scroll >>> lastItem:" + lastItem );
// Display 50 listitems, that is, 0-49. Because onScroll is triggered after "sliding" is executed, adapter. count <= 41 is used as the condition.
Int scrolllength = 101;
If (adapter. count <= scrolllength ){
If (firstVisibleItem + visibleItemCount = totalItemCount ){
Adapter. count + = 10;
Adapter. notifyDataSetChanged ();
ListView. setSelection (lastItem );
Int currentPage = adapter. count/10;
Toast. makeText (getApplicationContext (), "page" + currentPage + ", Toast. LENGTH_LONG). show ();
}
}
Else {
ListView. removeFooterView (loadingLayout );
}
}
@ Override
Public void onScrollStateChanged (AbsListView view, int scrollState ){
// TODO Auto-generated method stub
}
});
}
Class listViewAdapter extends BaseAdapter {
Int count = 10;/* starting amount */
Public int getCount (){
Return count;
}
Public Object getItem (int pos ){
Return pos;
}
Public long getItemId (int pos ){
Return pos;
}
Public View getView (int pos, View v, ViewGroup p ){
Log. I ("test", "getView >>> pos:" + pos );
TextView view;
If (v = null ){
View = new TextView (ListViewScroll. this );
} Else {
View = (TextView) v;
}
View. setText ("ListItem" + pos );
View. setTextSize (20f );
View. setGravity (Gravity. CENTER );
View. setHeight (60 );
Return view;
}
}
}
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<ListView android: cacheColorHint = "#00000000" android: id = "@ + id/myList"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
>
</ListView>
</LinearLayout>
Author "technical life"