Rewrite listview to pull and load from the drop-down refresh menu

Source: Internet
Author: User
Tags gety

The listview of Android does not have the refresh function. To implement this function, you must inherit the listview and override its method. At the same time, the onscrolllistener interface must be implemented.

To pull down and refresh, you need to add a head in the upper part of the original listview. listview itself provides the addheadview () method, but does not implement the refresh function. We can customize the head content, added the refresh function.

Pulling and loading more data is similar to pulling and refreshing data. You can add a footer to the lower part of listview. The specific implementation code is as follows:

Import Java. text. simpledateformat; import Java. util. date; import android. content. context; import android. util. attributeset; import android. view. layoutinflater; import android. view. motionevent; import android. view. view; import android. view. viewgroup; import android. view. animation. rotateanimation; import android. widget. abslistview; import android. widget. imageview; import android. widget. listview; import android. W Idget. abslistview. onscrolllistener; import android. widget. progressbar; import android. widget. textview; public class reflashlistview extends listview implements onscrolllistener {view header; view footer; int headerheight; int firstvisibleitem; // the position of the current first visible item Boolean isremark; // mark the first itemint starty; // int state of the Y value at press time; // int scrollstate of the current State; // final int none = 0; // final int pull = 1 ;/ /Pull-down refresh status final int relese = 2; // release status final int reflashing = 3; // refresh status ireflashlistener; int totalitemcount; int lastvisibleitem; Boolean isloading; // loading private textview txtviewloading = NULL; private progressbar prgbarloading = NULL; Public reflashlistview (context) {super (context); initview (context ); // todo auto-generated constructor stub} public reflashlistview (context Co Ntext, attributeset attrs) {super (context, attrs); initview (context); // todo auto-generated constructor stub} public reflashlistview (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); initview (context); // todo auto-generated constructor stub} // initialize the interface and add the top layout to listviewpublic void initview (context) {layoutinflater Inflater = layoutinflater. from (context); header = Inflater. inflate (R. layout. header_layout, null); footer = Inflater. inflate (R. layout. footer_layout, null); footer. findviewbyid (R. id. load_layout ). setvisibility (view. gone); measureview (header); headerheight = header. getmeasuredheight (); toppadding (-headerheight); this. addheaderview (header); this. addfooterview (footer); this. setonscrolllistener (this); txtviewloading = (textview) footer. findviewbyid (R. id. text_loadin G); prgbarloading = (progressbar) footer. findviewbyid (R. id. loading_progress);} // set the top margin of the header layout private void toppadding (INT toppadding) {header. setpadding (header. getpaddingleft (), toppadding, header. getpaddingright (), header. getpaddingbottom (); header. invalidate ();} // width occupied by the notification parent layout, high private void measureview (view) {viewgroup. layoutparams P = view. getlayoutparams (); If (P = NULL) {P = new viewgroup. lay Outparams (viewgroup. layoutparams. match_parent, viewgroup. layoutparams. wrap_content);} int width = viewgroup. getchildmeasurespec (0, 0, P. width); int height; int tempheight = P. height; If (tempheight> 0) {Height = measurespec. makemeasurespec (tempheight, measurespec. exactly);} else {Height = measurespec. makemeasurespec (tempheight, measurespec. unspecified);} view. measure (width, height) ;}@ overridepublic void Onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {This. firstvisibleitem = firstvisibleitem; this. totalitemcount = totalitemcount; this. lastvisibleitem = firstvisibleitem + visibleitemcount;} @ overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {This. scrollstate = scrollstate; If (totalitemcount = lastvisibleitem & scrollstate = scroll_state_id Le) {If (! Isloading) {isloading = true; footer. findviewbyid (R. id. load_layout ). setvisibility (view. visible); ireflashlistener. onload () ;}}@ overridepublic Boolean ontouchevent (motionevent eV) {Switch (ev. getaction () {Case motionevent. action_down: If (firstvisibleitem = 0) {isremark = true; starty = (INT) eV. gety ();} break; Case motionevent. action_move: onmove (EV); break; Case motionevent. action_up: If (State = relese) {s Tate = reflashing; // refresh the data ireflashlistener. onreflash (); reflashviewbystate ();} else if (State = pull) {state = none; isremark = false; reflashviewbystate ();} break;} return Super. ontouchevent (EV);}/** operations in moving process */private void onmove (motionevent e) {int Tempy = (INT) E. gety (); int Space = Tempy-starty; int toppadding = space-headerheight; If (! Isremark) {return;} switch (state) {case none: If (space> 0) {state = pull; reflashviewbystate () ;}break; case pull: toppadding (toppadding ); if (space> headerheight + 30 & scrollstate = scroll_state_touch_scroll) {state = relese; reflashviewbystate () ;}break; Case relese: toppadding (toppadding ); if (space  


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.