The first thing to know is that the refresh has three states,
1 is the drop-down.
2 is release refresh
3 is refreshing
Another important thing is the callback interface, which is what the outside world needs to do when it is being refreshed.
Then the outside world resets the state.
The callback interface requires three properties,
Private Onreflisner listener;
public void Setonreflisner (Onreflisner listener) {
This.listener = listener;
}
Callback Interface
public interface onreflisner{
void Setpullrfe ();
}
PackageCom.example.listviewf5;ImportJava.text.SimpleDateFormat;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.view.View;Importandroid.view.animation.RotateAnimation;ImportAndroid.widget.ImageView;ImportAndroid.widget.ListView;ImportAndroid.widget.ProgressBar;ImportAndroid.widget.TextView; Public classMylistviewextendsListView {PrivateView v; Private intHeadheight;//the height of the head Private intDownY;//the y-coordinate when pressed Private Final intPull_ref = 0;//Drop down Private Final intRel_ref = 1;//Release Refresh Private Final intrefing = 2;//Refresh in Private intCurrentState =Pull_ref; PrivateTextView TV; PrivateTextView Tvtiem; PrivateImageView img; PrivateProgressBar PB; //Rotate Animation Privaterotateanimation upanimation,downanimation; PublicMylistview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); //TODO auto-generated Constructor stubInitview (context); } PublicMylistview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stubInitview (context); } PublicMylistview (Context context) {Super(context); //TODO auto-generated Constructor stubInitview (context); } /*** Load Top Layout file * *@paramContext*/ Private voidInitview (Context context) {//Layoutinflater in =layoutinflater.from (context); //v = in.inflate (r.layout.head,null);v = view.inflate (context, R.layout.head,NULL); This. Addheaderview (v); V.measure (0, 0);//notification system measuring wide heightHeadheight = V.getmeasuredheight ();//get the measured heightV.setpadding (0,-headheight, 0, 0);//to hide the head is to set the paddingtop to a negative heightTV=(TextView) V.findviewbyid (R.ID.HEAD_TV); Tvtiem=(TextView) V.findviewbyid (r.id.head_tvtime); IMG=(ImageView) V.findviewbyid (r.id.head_img); PB=(ProgressBar) V.findviewbyid (R.ID.PB); Initheadrotateanimation (); } Private voidinitheadrotateanimation () {upanimation=NewRotateanimation (0,-180, Rotateanimation.relative_to_self,0.5f, Rotateanimation.relative_to_self,0.5f); Upanimation.setduration (300); Upanimation.setfillafter (true); Downanimation=NewRotateanimation (-180,-360, Rotateanimation.relative_to_self,0.5f, Rotateanimation.relative_to_self,0.5f); Downanimation.setduration (300); Downanimation.setfillafter (true); } @Override Public Booleanontouchevent (motionevent ev) {Switch(Ev.getaction ()) { CaseMotionEvent.ACTION_DOWN:downY= (int) ev.gety (); Break; CaseMotionevent.action_move://if it is refreshing the state slide is not if(currentstate==refing) { Break; } intDeltaY = (int) (Ev.gety ()-DownY);//get a moving distance intCurrentheight =-headheight +DeltaY; //determines whether the current distance is greater than headheight and shows the first position if(Currentheight >-headheight && getfirstvisibleposition () = = 0) {v.setpadding (0, currentheight, 0, 0);//Show your head . if(currentheight >= 0 && CurrentState = =pull_ref) {CurrentState=Rel_ref; Refheadview (); } Else if(Currentheight < 0 && CurrentState = =rel_ref) {CurrentState=Pull_ref; Refheadview (); } return true;//Intercept events do not let the ListView handle } Break; Casemotionevent.action_up://determine if the status is currently being refreshed if(currentstate==pull_ref) {v.setpadding (0,-headheight, 0, 0); }Else if(currentstate==rel_ref) {CurrentState=refing; V.setpadding (0,0, 0, 0); Refheadview (); if(listener!=NULL) {Listener.setpullrfe (); } } Break; } return Super. ontouchevent (EV); } Private voidRefheadview () {Switch(currentstate) { CasePULL_REF:tv.setText ("Drop-down Refresh"); Img.startanimation (downanimation); Break; CaseREL_REF:tv.setText ("Release Refresh"); Img.startanimation (upanimation); Break; CaseREFING:tv.setText ("Refreshing ..."); Img.setvisibility (View.gone); Img.clearanimation (); Pb.setvisibility (view.visible); Break; default: Break; } } //Refresh complete to reset the control and state Public voidCompleteref () {v.setpadding (0,-headheight, 0, 0);//Show your head .Currentstate=Pull_ref; Tv.settext ("Drop-down Refresh"); Img.setvisibility (view.visible); Pb.setvisibility (View.gone); SimpleDateFormat Sdateformat=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); String Date= Sdateformat.format (Newjava.util.Date ()); Tvtiem.settext ("Last refresh:" +date); } PrivateOnreflisner Listener; Public voidSetonreflisner (Onreflisner listener) { This. Listener =Listener; } //Callback Interface Public Interfaceonreflisner{voidSetpullrfe (); }}
PackageCom.example.listviewf5;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportCom.example.listviewf5.MyListView.OnRefLisner;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateMylistview LV; PrivateList<map<string, object>>list; PrivateSimpleadapter sa; PrivateHandler Handler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {sa.notifydatasetchanged (); Lv.completeref (); }; }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); LV=(Mylistview) Findviewbyid (r.id.lv); List=NewArraylist<map<string,object>>(); for(inti = 0; I <20; i++) {Map<string, object> map =NewHashmap<string, object>(); Map.put ("Key1", "The Gods and Horses are clouds"); Map.put ("Key2", "Heheda"); List.add (map); } SA=NewSimpleadapter ( This, LIST,R.LAYOUT.ITEM_LV,Newstring[]{"Key1", "Key2"},New int[]{R.ID.TV,R.ID.TV2}]; Lv.setadapter (SA); Lv.setonreflisner (NewOnreflisner () {@Override Public voidSetpullrfe () {Map<string, object> map =NewHashmap<string, object>(); Map.put ("Key1", "drop-down updated data"); Map.put ("Key2", "Hehedaheheda"); List.add (0, map); Handler.sendemptymessagedelayed (0,3000); } }); }}
Android ListView drop-down refresh feature implementation