This blog for you to introduce a common Android feature--listview drop-down refresh (refer to from others blog, url forgotten, read his code himself to understand the comments, hope to help everyone):
The first time the dropdown did not let go when the phone displays such an interface:
The following code is a custom tie-like control:
Package Com.dhsr.smartid.view;import Android.content.context;import Android.util.attributeset;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.view.animation.animation;import Android.view.animation.rotateanimation;import Android.widget.ImageView; Import Android.widget.linearlayout;import Android.widget.progressbar;import Android.widget.textview;import com.example.sirelinkscanapp.r;/** * Custom control, complete the dropdown when the ListView "icon and hints are refreshing ..." layout * * @author CYF * */public class Xlistviewhea Der extends LinearLayout {private linearlayout mcontainer;//picture private ImageView marrowimageview;//circular progress bar private ProgressBar mprogressbar;private TextView mhinttextview;//State private int mstate = state_normal;//animation private Animation MRo Tateupanim;private Animation mrotatedownanim;private Final int rotate_anim_duration = 180;//normal public final static int STA Te_normal = 0;//ready to refresh public final static int state_ready = 1;//Refresh in public final static int state_refreshing = 2;pubLic Xlistviewheader (Context context) {super (context); Initview (context);} /** * @param context * @param attrs */public Xlistviewheader (context context, AttributeSet Attrs) {Super (context, attrs); I Nitview (context);} /** * "Release to refresh ... Refresh ... "Layout * * @param context */private void Initview (Context context) {///initial case, set dropdown refresh view height to 0linearlayout.layoutparams l p = new Linearlayout.layoutparams (layoutparams.match_parent, 0); Mcontainer = (linearlayout) layoutinflater.from ( context). Inflate (R.layout.xlistview_header, NULL);//Load View AddView (Mcontainer, LP);//Center Mode setgravity (Gravity.bottom) ;//Initialize control Marrowimageview = (ImageView) Findviewbyid (r.id.xlistview_header_arrow); Mhinttextview = (TextView) Findviewbyid (r.id.xlistview_header_hint_textview); Mprogressbar = (ProgressBar) Findviewbyid (r.id.xlistview_header _progressbar);//Set Animation Mrotateupanim = new Rotateanimation (0.0f, -180.0f,animation.relative_to_self, 0.5f, animation.relative_to_self,0.5f); mrotateupanim.setduration (rotate_anim_duration); mRotateUpAnim.setFillafter (true); Mrotatedownanim = new Rotateanimation ( -180.0f, 0.0f,animation.relative_to_self, 0.5f, animation.relative_to_self,0.5f); mrotatedownanim.setduration (rotate_anim_duration); Mrotatedownanim.setfillafter (TRUE);} /** * performs the corresponding function according to the drop-down status, starting and stopping the corresponding animation * * @param state */public void SetState (int.) {if (state = = mstate) return;if = = state_refreshing) {//Show Progress marrowimageview.clearanimation (); marrowimageview.setvisibility (View.INVISIBLE); Mprogressbar.setvisibility (view.visible);} else {//show arrows Picture marrowimageview.setvisibility (view.visible); mprogressbar.setvisibility (view.invisible);} Switch (state) {case State_normal:if (mstate = = State_ready) {//Start animation marrowimageview.startanimation (Mrotatedownanim);} Refresh in if (mstate = = state_refreshing) {//Clear animation marrowimageview.clearanimation ();} Mhinttextview.settext (r.string.xlistview_header_hint_normal); Break;case state_ready:if (mState! = STATE_READY) { Marrowimageview.clearanimation (); marrowimageview.startanimation (Mrotateupanim); Mhinttextview. SetText (R.string.xlistview_header_hint_ready);} Break;case STATE_REFRESHING:mHintTextView.setText (r.string.xlistview_header_hint_loading); break;default:}mstate = State;} public void setvisiableheight (int height) {if (height < 0) height = 0; Linearlayout.layoutparams LP = (linearlayout.layoutparams) mcontainer.getlayoutparams (); lp.height = height; MCONTAINER.SETLAYOUTPARAMS (LP);} public int getvisiableheight () {return mcontainer.getheight ();}}
Next you need to customize your own ListView to inherit the ListView with Android itself, to easily add a new method yourself.
Package Com.dhsr.smartid.view;import Android.content.context;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.view.ViewTreeObserver.OnGlobalLayoutListener; Import Android.view.animation.decelerateinterpolator;import Android.widget.abslistview;import Android.widget.abslistview.onscrolllistener;import Android.widget.listadapter;import Android.widget.ListView; Import Android.widget.relativelayout;import Android.widget.scroller;import Android.widget.textview;import com.example.sirelinkscanapp.r;/** * Custom ListView * * @author CYF * */public class Xlistview extends ListView implements O Nscrolllistener {private final String TAG = "Xlistview";p rivate float mlasty = -1;private Scroller mscroller;//Slide Private Onscrolllistener mscrolllistener;//Create a custom control that listens to the outside private Ixlistviewlistener mlistviewlistener;//just defined private Xlistviewheader mheaderview;private relativelayout mheaderviewcontent;private TextView mheadertimeview;private int MheaderviewhEight Header View ' s heightprivate boolean menablepullrefresh = true;private Boolean mpullrefreshing = false; Is Refreashing.private Boolean Menablepullload;private boolean mpullloading;private boolean misfooterready = false;// Total list items, used to detect are at the bottom of listview.private int mtotalitemcount;//for Mscroller, scroll back fr Om header or footer.private int mscrollback;private final static int scrollback_header = 0;private final static int SCROLL Back_footer = 1;private final static int scroll_duration = 400; Scroll back durationprivate final static int pull_load_more_delta = 50; When pulling up >= 50px//at bottom, trigger//load more.private final static float Offset_radio = 1.8f; Support for IOS like pull//feature./** * @param context */public Xlistview (context context) {super (context); Initwithcontex T (context);} Public Xlistview (context context, AttributeSet Attrs) {Super (context, attrs); Initwithcontext (context);} Public Xlistview (context context, AttrIbuteset attrs, int defstyle) {Super (context, attrs, Defstyle); Initwithcontext (context);} /** * Initialize Control * * @param context */private void Initwithcontext (context context) {Mscroller = new Scroller (context, new DEC Elerateinterpolator ()); Super.setonscrolllistener (this); Mheaderview = new Xlistviewheader (context); Mheaderviewcontent = (relativelayout) Mheaderview.findviewbyid (r.id.xlistview_header_content); MHeaderTimeView = ( TextView) Mheaderview.findviewbyid (r.id.xlistview_header_time); Addheaderview (Mheaderview, NULL, FALSE); Mheaderview.getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () {@Overridepublic void Ongloballayout () {mheaderviewheight = Mheaderviewcontent.getheight (); Getviewtreeobserver (). Removeglobalonlayoutlistener (this);}});} @Overridepublic void Setadapter (ListAdapter adapter) {//Make sure xlistviewfooter are the last footer view, and only add O Nce.if (Misfooterready = = False) {if (menablepullload) {misfooterready = true;//addfooterview (Mfooterview);}} SUper.setadapter (adapter);} public void Setpullrefreshenable (Boolean enable) {Menablepullrefresh = Enable;if (!menablepullrefresh) {//disable, hide The contentmheaderviewcontent.setvisibility (view.invisible);} else {mheaderviewcontent.setvisibility (view.visible);}} /** * Stop flushing */public void Stoprefresh () {if (mpullrefreshing = = true) {mpullrefreshing = False;resetheaderheight ();}} You can make the drop-down refresh public void Startrefresh () {if (mpullrefreshing = = False) {mpullrefreshing = true when entering activity) Mheaderview.setstate (xlistviewheader.state_refreshing); mheaderview.setvisiableheight; if (MListViewListener!) = null) {Mlistviewlistener.onrefresh ();}}} /** * Stop load more, reset footer view. */public void Stoploadmore () {if (mpullloading = = true) {mpullloading = false;}} /** * Set Last Refresh time * * @param time */public void Setrefreshtime (String time) {Mheadertimeview.settext (time);} private void invokeonscrolling () {if (Mscrolllistener instanceof Onxscrolllistener) {Onxscrolllistener L = (OnXScrollLisTener) mscrolllistener;l.onxscrolling (this);}} private void Updateheaderheight (float delta) {mheaderview.setvisiableheight (int) delta+ Mheaderview.getvisiableheight ()); if (Menablepullrefresh &&!mpullrefreshing) {//Not in refresh state, update arrow if ( Mheaderview.getvisiableheight () > Mheaderviewheight) {mheaderview.setstate (xlistviewheader.state_ready);} else { Mheaderview.setstate (xlistviewheader.state_normal);} if (mpullloading) {//disable, hide the contentmheaderviewcontent.setvisibility (view.invisible);} else { Mheaderviewcontent.setvisibility (view.visible);}} SetSelection (0); Scroll to top each time}/** * Reset header view ' s height. */private void Resetheaderheight () {int height = mheaderview.getvisiableheight (); if (height = = 0)//Not visible.return;// Refreshing and header isn ' t shown fully. Do Nothing.if (mpullrefreshing && height <= mheaderviewheight) {return;} int finalheight = 0; Default:scroll back to dismiss header.//was refreshing, just scroll back to show all the Header.if (mpullrefreshing && height > mheaderviewheight) {finalheight = Mheaderviewheight;} Mscrollback = Scrollback_header;mscroller.startscroll (0, height, 0, finalheight-height,scroll_duration);//Trigger Computescrollinvalidate ();} /** * Touch screen Monitor */@Overridepublic boolean ontouchevent (motionevent ev) {if (mlasty = =-1) {mlasty = Ev.getrawy ();} Switch (ev.getaction ()) {Case MotionEvent.ACTION_DOWN:mLastY = Ev.getrawy (); Break;case MotionEvent.ACTION_MOVE:final float DeltaY = Ev.getrawy ()-mlasty;//dlog.i (TAG, "DeltaY is" + deltay); mlasty = Ev.getrawy (); if (Getfirstvisiblepositi On () = = 0&& (mheaderview.getvisiableheight () > 0 | | deltay > 0)) {//The first item is showing, header has s Hown or Pull down.updateheaderheight (Deltay/offset_radio); invokeonscrolling ();} else if (getlastvisibleposition () = = mTotalItemCount-1) {//last item, already pulled-up or want-to-pull-up.} Break;default:mlasty =-1; Resetif (getfirstvisibleposition () = = 0) {//Invoke Refreshif (!mpullrefreshing && menablepullrefresh &&!mpullloading&& mheaderview.getvisiableheight ( ) > Mheaderviewheight) {mpullrefreshing = True;mheaderview.setstate (xlistviewheader.state_refreshing);//DLOG.I ( TAG, "Invoke Refresh"), if (mlistviewlistener! = null) {//At this time perform a new method of brushing Mlistviewlistener.onrefresh ();}} Resetheaderheight ();} else if (getlastvisibleposition () = = mTotalItemCount-1) {//Invoke load More.if (!mpullloading && Menablepullloa D &&!mpullrefreshing) {//DLOG.I (TAG, "Invoke load More");}} break;} return super.ontouchevent (EV);} @Overridepublic void Computescroll () {if (Mscroller.computescrolloffset ()) {if (Mscrollback = = Scrollback_header) { Mheaderview.setvisiableheight (Mscroller.getcurry ());} else {}postinvalidate (); invokeonscrolling ();} Super.computescroll ();} @Overridepublic void Setonscrolllistener (Onscrolllistener l) {mscrolllistener = l;} /** * Callback when scrolling, callback 2-3 times, the finger does not leave the callback 2 times, scrollstate = 2 This time not callback 1th time: scrollstate = * Scroll_state_touch_scroll(1) Scrolling 2nd time: scrollstate = * Scroll_state_fling (2) The finger did the throwing action (the finger left the screen, a hard slide) 3rd time: scrollstate = * Scroll_state_idle (0) Stop scrolling */@Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {if (Mscrolllistener! = null) { Mscrolllistener.onscrollstatechanged (view, scrollstate);} Automatically load more when sliding to the bottom. You can also disable this logic if (getlastvisibleposition () = = mtotalitemcount-1&& Scrollstate = = Scroll_state_idle) {if (! Mpullloading && menablepullload &&!mpullrefreshing) {//DLOG.I (TAG, "Invoke load More");}}} /** * Callback when scrolling, stop the callback when the scroll is stopped, callback once when clicked */@Overridepublic void onscroll (abslistview view, int firstvisibleitem,int VisibleItemCount, int totalitemcount) {//Send to user ' s listenermtotalitemcount = totalitemcount;if (Mscrolllistener! = N ull) {mscrolllistener.onscroll (view, Firstvisibleitem, Visibleitemcount,totalitemcount);}} public void Setxlistviewlistener (Ixlistviewlistener l) {mlistviewlistener = l;} /** * Monitor the sliding events of the listview * * @author CYF * */public interface OnxscrolllistenER extends Onscrolllistener {public void onxscrolling (view view); /** * Custom Interface */public interface Ixlistviewlistener {///drop-down Refresh time the method public void Onrefresh ();//The method that is executed when the pull-up load is public void Onloadm Ore ();}}
The ListView in the Mainactivity XML file does not use the Android ListView, using the Custom ListView (add-in name)
Package Com.test.andy;import Java.util.arraylist;import Com.test.andy.view.xlistview;import Com.test.andy.view.xlistview.ixlistviewlistener;import Com.test.andy.r;import Android.app.Activity;import Android.os.bundle;import android.os.handler;import android.widget.arrayadapter;/** * XListViewActivity * The implementation of the Ixlistviewlistener interface is to achieve a drop-down refresh method such as * @author CYF * */public class Xlistviewactivity extends Activity implements Ixlist Viewlistener {//Custom listviewprivate Xlistview mlistview;private arrayadapter<string> mAdapter;private arraylist<string> items = new arraylist<string> ();p rivate Handler mhandler;private int start = 0;private int R efreshcnt = 0;/** Called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main);//Initialize Load 20 data geneitems (); Mlistview = (Xlistview) Findviewbyid (R.id.xlistview); Mlistview.setpullloadenable (true); madapter = new Arrayadapter<string> (this, R.layout.list_item, items); Mlistview.setadapter (Madapter);//mlistview.setpullrefreshenable (false); Mlistview.setxlistviewlistener (this); Mhandler = new Handler (); Mlistview.startrefresh ();} private void Geneitems () {for (int i = 0; I! = ++i) {items.add ("Refresh cnt" + (++start));}} private void OnLoad () {Mlistview.stoprefresh (); Mlistview.stoploadmore (); Mlistview.setrefreshtime ("just");} @Overridepublic void Onrefresh () {mhandler.postdelayed (new Runnable () {@Overridepublic void run () {start = ++refreshcnt; Items.clear (); Geneitems ();//madapter.notifydatasetchanged (); madapter = new Arrayadapter<string> ( Xlistviewactivity.this, R.layout.list_item, items); Mlistview.setadapter (Madapter); OnLoad ();}}, 2000);} @Overridepublic void Onloadmore () {mhandler.postdelayed (new Runnable () {@Overridepublic void run () {geneitems (); Madapter.notifydatasetchanged (); OnLoad ();}}, 2000);}}
Important code is summed up here, I hope to bring you help, thank you.
Android-custom Control Implementation ListView drop-down Refresh