Listview achieves paging loading like Android Market

Source: Internet
Author: User
In the past few days, we have studied how to implement paging loading and rolling loading in listview. We found that the onscroll method of listview can be used for implementation. Code Listviewscroll. javaview plaincopy to clipboardprint?
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;
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 = 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 = 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;

}
}
}
Main. xml

View plaincopy to clipboardprint?
<? 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>

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.