<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Long time No blog, today I will write a ListView of the drop-down to share to everyone, hope to learn or work with you to help, in fact, we all know, in the current app, the drop-down refresh is more commonly used functions, used to the place too much, so, Today I also combine their own ideas to give home truth, so good, next, let us learn together, let you in minutes to write your own custom ListView drop-down Refresh </span>
One: the realization of ideas
1. Implement add the ListView to the head view, that is, the drop-down to appear is the head layout
2. Hide the view of the head (this requires special attention)
3. Set the slider to monitor the ListView (to determine if the head)
4. Set the gesture event, move the finger when the action done
Two: Source code example
1. Custom Controls
Package Com.zengtao.refreshview;import Java.text.simpledateformat;import Java.util.date;import Android.annotation.suppresslint;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.abslistview.onscrolllistener;import Android.widget.imageview;import Android.widget.ListView; Import Android.widget.progressbar;import android.widget.textview;import com.zengtao.pulltorefreshtest.r;/** * Dropdown Refresh ListView * * @author Zengtao March 17, 2015 PM 7:13:55 */public Class Refreshableview extends ListView implements Onscroll Listener {/** drop-down refreshed head */private View headerview;/** head arrow */private ImageView arrawimage;/** update after refresh time */private TextView lastupdattime;/** refresh a state */private TextView statemessage;/** Load Progress */private ProgressBar progressbar;/** Get the height of the header file */ private int Headerheight;/** the current first visible position */private int firstvisibleitem;/** Marks whether the current can refresh */private boolean isreflash;/** pressed y-value */private int starty; /** status */private int state;/** normal state */private final int NONE = 0;/** hint drop-down refresh status */private final int pull = 1;/** prompt release Refresh status */private final int refpull = 2;/** hint is refreshing */private final int LOADING = 3;/** ListView current scroll position */private int scorellstate p;/** Data Callback interface */private Ireflashlinster ireflashlinster;public Refreshableview (context context) {super (context); Initview (context);} Public Refreshableview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Initview ( context);} Public Refreshableview (context context, AttributeSet Attrs) {Super (context, attrs); Initview (context);} /** * Function: Initialize the drop-down refreshed header * * @param context *: Context */@SuppressLint ("Inflateparams") private void Initview (Context Co ntext) {Headerview = Layoutinflater.from (context). Inflate (R.layout.pull_to_refresh, NULL);//initialization attempted arrawimage = ( ImageView) Headerview.findviewbyid (R.ID.IV_arrow); statemessage = (TextView) Headerview.findviewbyid (r.id.pull_state); lastupdattime = (TextView) Headerview.findviewbyid (r.id.lastupdatetime);p Rogressbar = (progressBar) Headerview.findviewbyid (R.id.progress_ bar);//notify the parent layout to get to how high, otherwise 0measureView (headerview); headerheight = Headerview.getmeasuredheight (); TopPadding (- Headerheight); This.addheaderview (Headerview); This.setonscrolllistener (this);} /** * Notifies the parent layout how much height and width I should take * * @param view *: Attempt to layout */private void Measureview (view view) {Viewgroup.layo Utparams p = view.getlayoutparams (), if (p = = null) {p = new Viewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);} int width = viewgroup.getchildmeasurespec (0, 0, p.width), int tempheight = P.height;int height;if (tempheight > 0) {Heig HT = Measurespec.makemeasurespec (tempheight,measurespec.exactly);} else {height = measurespec.makemeasurespec (0, measurespec.unspecified);} View.measure (width, height);} /** * Set the top margin of the header layout * * @param tOpheight *: Top margin height */private void toppadding (int toppadding) {headerview.setpadding (Headerview.getpaddingleft () , Toppadding,headerview.getpaddingright (), Headerview.getpaddingbottom ()); Headerview.invalidate ();} @Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {this.scorellstatep = scrollstate;} @Overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) { This.firstvisibleitem = Firstvisibleitem;} @SuppressLint ("Clickableviewaccessibility") @Overridepublic boolean ontouchevent (motionevent ev) {switch ( Ev.getaction ()) {case motionevent.action_down://ListView has slipped to head if (Firstvisibleitem = = 0) {Isreflash = True;starty = (int ) ev.gety ();} Break;case motionevent.action_move://operation OnMove (EV) when moving, Break;case motionevent.action_up://when it is released see what state if ( state = = Refpull) {state = LOADING;//Can load data reflashviewbysate (); Reflashcomplete (); Ireflashlinster.reflash ();} else if (state = = pull) {state = None;isreflash = False;reflashviewbysate ();} break;} return super.ontouchevent (EV);} /** * operation of the finger when moving * * @param ev */private void OnMove (motionevent ev) {//is not able to refresh if (!isreflash) {return;} The current moved position int tempy = (int) ev.gety (), int spance = tempy-starty;//Top layout is 1.1 point display, so to keep setting the top layout int toppadding = spance- Headerheight;switch (state) {case none://when the user is pulled down: Hint dropdown status if (Spance > 0) {state = pull;} Break;case pull:toppadding (toppadding);//When the user drop-down is more than certain and is in a sliding scroll: Prompt to release the refresh status if (Spance > Headerheight + 20&& Scorellstatep = = scroll_state_touch_scroll) {state = Refpull;reflashviewbysate ();} Break;case refpull:toppadding (toppadding);//When the user rolls back below a certain height: prompt to drop the refresh status if (Spance < Headerheight +) {state = pull; Reflashviewbysate ();} else if (spance <= 0) {state = None;isreflash = False;reflashviewbysate ();}}} /** * Based on status: in go to decide whether to refresh data and load layout */private void Reflashviewbysate () {//animation effect Rotateanimation animation = new Rotateanimation (0, 1 80,rotateanimation.relative_to_self, 0.5f,rotateanimation.relative_to_sELF, 0.5f); animation.setduration; Animation.setfillafter (true);//animation effect rotateanimation Animation1 = new Rotateanimation (0,rotateanimation.relative_to_self, 0.5f,rotateanimation.relative_to_self, 0.5f); Animation1.setduration (+); Animation1.setfillafter (true); switch (state) {case none:toppadding (-headerheight); Arrawimage.clearanimation (); break;case PULL:arrawImage.setVisibility (view.visible);p rogressbar.setvisibility ( View.gone); Statemessage.settext ("Pull down can be refreshed"); Arrawimage.clearanimation (); arrawimage.setanimation (Animation1); Break;case REFPULL:arrawImage.setVisibility (view.visible);p rogressbar.setvisibility (View.gone); Statemessage.settext ("Release can be Refreshed"); Arrawimage.clearanimation (); arrawimage.setanimation (animation); break;case Loading:toppadding (a);p rogressbar.setvisibility (view.visible); arrawimage.setvisibility (View.GONE); Statemessage.settext ("refreshing ..."); Arrawimage.clearanimation (); break;}} /** * After the refresh is complete, change the refresh time */@SuppressLint ("SimpleDateFormat") public void Reflashcomplete () {state = NONE;Isreflash = False;reflashviewbysate (); SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD Day HH:MM:SS");D ate date = new Date (System.currenttimemillis ()); String time = Sdf.format (date); Lastupdattime.settext (time);} Public interface Ireflashlinster {public void Reflash ();} public void Setireflashlinster (Ireflashlinster ireflashlinster) {this.ireflashlinster = Ireflashlinster;}
2.Maintivity use in the main interface
Package Com.zengtao.pulltorefreshtest;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.view.window;import Android.widget.arrayadapter;import Com.zengtao.refreshview.refreshableview;import Com.zengtao.refreshview.refreshableview.ireflashlinster;public Class Mainactivity extends Activity implements Ireflashlinster {private Refreshableview listview;private Arrayadapter <String> adapter;private string[] str = {"The Fool is the King 1", "The Idiot is the King 2", "Text 3", "The Fool is the King 4", "The Idiot is the King 5", "The Idiot is the King 6", "The Idiot is is kingly 7 "," The Fool is the King 8 "," The Idiot is the King 9 "," The Idiot is the King 10 "," The Idiot is the king of 11 "," The Idiot is the King 12 "," The Idiot is the King 13 "," The Idiot is the King 14 "," The Idiot is the King 1 "," Stupid is the King 2 " Force is the King 3 "," The Fool is the King 4 "," The Idiot is the King 5 "," The Idiot is the King 6 "," The Fool is the King 7 "," The Idiot is the King 8 "," The Idiot is the King 9 "," The Idiot is the King 10 "," The Idiot is the King 11 "," Stupid is the King 12 ", is the kingly 13 "," The Idiot is the King "}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); InitView ();} Private void Initview () {ListView = (Refreshableview) Findviewbyid (R.id.listview); Listview.setireflashlinster (this); Adapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1, str); Listview.setadapter (Adapter);} @Overridepublic void Reflash () {Handler Handler = new Handler (); handler.postdelayed (new Runnable () {@Overridepublic void Run () {}}, 3000);}}
The above changes can complete a custom ListView customization and use, I hope to help you, I write bad, please do not blame.
Android ListView drop-down Refresh