This example describes the implementation of the pull-Refresh method under Android programming. Share to everyone for your reference, specific as follows:
Now the Android application has a lot of drop-down refresh, today I study the relevant information
public class Mylistview extends ListView implements Onscrolllistener {private static final String TAG = "ListView";
Private final static int release_to_refresh = 0;
Private final static int pull_to_refresh = 1;
Private final static int refreshing = 2;
Private final static int done = 3;
Private final static int LOADING = 4;
The actual padding distance is proportional to the offset of the interface with the private final static int RATIO = 3;
Private Layoutinflater Inflater;
Private LinearLayout Headview;
Private TextView Tipstextview;
Private TextView Lastupdatedtextview;
Private ImageView Arrowimageview;
Private ProgressBar ProgressBar;
Private rotateanimation animation;
Private Rotateanimation reverseanimation;
Used to ensure that the value of Starty is only recorded once in a full touch event as private Boolean isrecored;
private int headcontentwidth;
private int headcontentheight;
private int starty;
private int firstitemindex;
private int state;
Private Boolean isback;
Private Onrefreshlistener Refreshlistener;
Private Boolean isrefreshable; Public MylistvieW (context context) {super (context);
Init (context);
Public Mylistview (context, AttributeSet attrs) {Super (context, attrs);
Init (context);
private void init (context context) {Setcachecolorhint (Context.getresources (). GetColor (r.color.transparent));
Inflater = Layoutinflater.from (context);
Headview = (linearlayout) inflater.inflate (r.layout.head, NULL);
Arrowimageview = (ImageView) headview. Findviewbyid (R.id.head_arrowimageview);
Arrowimageview.setminimumwidth (70);
Arrowimageview.setminimumheight (50);
ProgressBar = (ProgressBar) headview. Findviewbyid (R.id.head_progressbar);
Tipstextview = (TextView) Headview.findviewbyid (R.id.head_tipstextview);
Lastupdatedtextview = (TextView) headview. Findviewbyid (R.id.head_lastupdatedtextview);
Measureview (Headview);
Headcontentheight = Headview.getmeasuredheight ();
Headcontentwidth = Headview.getmeasuredwidth ();
Headview.setpadding (0,-1 * headcontentheight, 0, 0); Headview.invalidatE ();
LOG.V ("Size", "width:" + headcontentwidth + "Height:" + headcontentheight);
Addheaderview (Headview, NULL, FALSE);
Setonscrolllistener (this); Animation = new Rotateanimation (0, -180, rotateanimation.relative_to_self, 0.5f, Rotateanimation.relative_to_self,
0.5f);
Animation.setinterpolator (New Linearinterpolator ());
Animation.setduration (250);
Animation.setfillafter (TRUE); Reverseanimation = new Rotateanimation ( -180, 0, Rotateanimation.relative_to_self, 0.5f, rotateanimation.relative_t
O_self, 0.5f);
Reverseanimation.setinterpolator (New Linearinterpolator ());
Reverseanimation.setduration (200);
Reverseanimation.setfillafter (TRUE);
state = done;
Isrefreshable = false; } public void Onscroll (Abslistview arg0, int firstvisiableitem, int arg2, int arg3) {Firstitemindex = firstvisiable
Item; public void onscrollstatechanged (Abslistview arg0, int arg1) {} public boolean ontouchevent (Motionevent event) {i F (isrefreshable) {sWitch (Event.getaction ()) {case MotionEvent.ACTION_DOWN:if (Firstitemindex = = 0 &&!isrecored) {IsR
Ecored = true;
Starty = (int) event.gety ();
LOG.V (TAG, "record the current position at down time");
} break; Case MotionEvent.ACTION_UP:if (State!= refreshing && State!= LOADING) {if (state = done) {//
Do nothing} if (state = = Pull_to_refresh) {state = done;
Changeheaderviewbystate ();
LOG.V (TAG, "from the Drop-down refresh state, to the done State");
} if (state = = Release_to_refresh) {state = refreshing;
Changeheaderviewbystate ();
Onrefresh ();
LOG.V (TAG, "by loosening the refresh state, to the done State");
} isrecored = false;
Isback = false;
Break
Case MotionEvent.ACTION_MOVE:int tempy = (int) event.gety ();
if (!isrecored && firstitemindex = = 0) {log.v (TAG, "record position at move Time");
Isrecored = true;
Starty = Tempy; } if (state!= refreshing && isrecored && State!= LOADING){//Ensure that the current position is always in the head during the setting of the padding, otherwise if when the list is out of the screen, the list will scroll at the same time on the push//can let go to refresh the IF (state = = Release_to_ref
Resh) {setselection (0); Pushed up, pushed to the screen enough to cover up the head, but it hasn't been pushed to the full extent of the cover-up if ((tempy-starty)/RATIO < Headcontentheight) && (te
Mpy-starty) > 0) {state = Pull_to_refresh;
Changeheaderviewbystate ();
LOG.V (TAG, "from the release of the refresh state to the Drop-down refresh State");
//All of a sudden pushed to the top else if (tempy-starty <= 0) {state = done;
Changeheaderviewbystate ();
LOG.V (TAG, "transition from release refresh state to done State"); //down, or not up to the top of the screen to cover up the head. else {//No special operation, just update paddingtop value on the line}//Not yet reached show loose
At the time of the refresh, done or Pull_to_refresh status if (state = = Pull_to_refresh) {setselection (0); Drop down to a status that can enter Release_to_refresh if ((tempy-starty)/RATIO >= headcontentheight) {state = Release_to_re
Fresh;
Isback = true;
Changeheaderviewbystate (); LOG.V (TAG, "Transition from done or Drop-down refresh state to release refresh ");
//pushed up to the top of else if (tempy-starty <= 0) {state = done;
Changeheaderviewbystate ();
LOG.V (TAG, "transition from done or drop-down refresh state to done State");
{if (Tempy-starty > 0) {state = Pull_to_refresh) {if)
Changeheaderviewbystate ();
}//Update Headview size if (state = = Pull_to_refresh) {headview.setpadding (0,-1 * headcontentheight
+ (Tempy-starty)/RATIO, 0, 0); }//Update Headview paddingtop if (state = = Release_to_refresh) {headview.setpadding (0, (tempy-starty)/RA
Tio-headcontentheight, 0, 0);
}} break;
} return Super.ontouchevent (event); ///When the state changes, call the method to update the interface private void Changeheaderviewbystate () {switch (states) {case Release_to_refresh:arrow
Imageview.setvisibility (view.visible);
Progressbar.setvisibility (View.gone); Tipstextview.setvisibility (view.visible);
Lastupdatedtextview.setvisibility (view.visible);
Arrowimageview.clearanimation ();
Arrowimageview.startanimation (animation);
Tipstextview.settext ("loosen refresh");
LOG.V (TAG, "Current state, release refresh");
Break
Case PULL_To_REFRESH:progressBar.setVisibility (View.gone);
Tipstextview.setvisibility (view.visible);
Lastupdatedtextview.setvisibility (view.visible);
Arrowimageview.clearanimation ();
Arrowimageview.setvisibility (view.visible);
is an if (isback) {Isback = False that is transformed by a release_to_refresh state;
Arrowimageview.clearanimation ();
Arrowimageview.startanimation (reverseanimation);
Tipstextview.settext ("Drop-down refresh");
else {tipstextview.settext ("Drop-down refresh");
LOG.V (TAG, "Current state, Drop-down refresh");
Break
Case REFRESHING:headView.setPadding (0, 0, 0, 0);
Progressbar.setvisibility (view.visible);
Arrowimageview.clearanimation ();
Arrowimageview.setvisibility (View.gone);
Tipstextview.settext ("refreshing ...");
Lastupdatedtextview.setvisibility (view.visible); LOG.V (TAG, "current state, refreshing ...");
Break
Case DONE:headView.setPadding (0,-1 * headcontentheight, 0, 0);
Progressbar.setvisibility (View.gone);
Arrowimageview.clearanimation ();
Arrowimageview.setimageresource (R.drawable.arrow);
Tipstextview.settext ("Drop-down refresh");
Lastupdatedtextview.setvisibility (view.visible);
LOG.V (TAG, "current state, done");
Break
} public void Setonrefreshlistener (Onrefreshlistener refreshlistener) {this.refreshlistener = Refreshlistener;
Isrefreshable = true;
Public interface Onrefreshlistener {public void Onrefresh ();
public void Onrefreshcomplete () {state = done;
Lastupdatedtextview.settext ("Last update:" + new Date (). toLocaleString ());
Changeheaderviewbystate ();
private void Onrefresh () {if (Refreshlistener!= null) {Refreshlistener.onrefresh (); }//This method is directly copied from a Drop-down refresh on the network demo, here is "estimated" headview width and height private void Measureview (View child) {VIEWGROUP.LAYOUTP
Arams p = child.getlayoutparams (); if (p = = null{p = new Viewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int childwidthspec = viewgroup.getchildmeasurespec (0, 0 + 0, p.width);
int lpheight = P.height;
int childheightspec;
if (Lpheight > 0) {childheightspec = Measurespec.makemeasurespec (Lpheight, measurespec.exactly);
else {childheightspec = Measurespec.makemeasurespec (0, measurespec.unspecified);
} child.measure (Childwidthspec, Childheightspec);
public void Setadapter (Baseadapter adapter) {Lastupdatedtextview.settext ("Last update:" + new Date (). toLocaleString ());
Super.setadapter (adapter);
}
}
I hope this article will help you with your Android programming.