Android-based pedometer (pedometer) explanation (vi)--listview dropdown Refresh Page

Source: Internet
Author: User
Tags gety

Pedometer (pedometer) the entire project source code, recently made a relatively large changes, may not have been downloaded before the run, interested friends can download to see (remember to help younger brother on GitHub to play a Star ~)

Https://github.com/296777513/pedometer

Today implementation of the implementation of the drop-down refresh function, the first few pages:


, today is the way to achieve this effect

First, analyze the ListView pull-down Refresh implementation mode

1, need to add top drop loading page

2, need to listen to Onscrolllistener to determine whether currently displayed in the top of the ListView

3, because the top of the pull-loading interface is to follow the gesture sliding state constantly changing the interface display, so we need to monitor the Ontouch event, to change the current state and interface display;

4. Load data according to current state

Here's the code for the Reflashlistview drop-down Refresh class

Package Com.example.listviewfreshdemo1;import Java.text.simpledateformat;import Java.util.date;import Android.annotation.suppresslint;import Android.content.context;import Android.util.attributeset;import Android.util.log;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;public class Reflashlistview extends ListView Implements Onscrolllistener {Private View header;//top layout file; int headerheight;//height of top layout file; int firstvisibleitem;// The position of the currently first visible item, int scrollstate;//ListView Current scrolling state, Boolean isremark;//tag, currently at the top of the ListView, and int starty;//the Y value when pressed; int state;//the current state; final int NONE = 0;//normal state; final int pull = 1;//hint drop-down state; final int relese = 2;//hint release status; final int ReflashiNG = 3;//Refresh state, private Ireflashlistener ireflashlistener;//Refresh Data Interface Public Reflashlistview (context context) {Super ( context);//TODO auto-generated Constructor Stubinitview (context);} Public Reflashlistview (context context, AttributeSet Attrs) {Super (context, attrs);//TODO Auto-generated constructor Stubinitview (context);} Public Reflashlistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//TODO Auto-g Enerated constructor Stubinitview (context);} /** * Initialize interface, add top layout file to ListView * * @param context */private void Initview (context context) {Layoutinflater Inflater = Lay Outinflater.from (context); header = Inflater.inflate (R.layout.header, NULL);//loading top layout file Measureview (header); Headerheight = Header.getmeasuredheight (); LOG.I ("tag", "headerheight =" + headerheight); toppadding (-headerheight); This.addheaderview (header); This.setonscrolllistener (this);} /** * Notify parent layout, Occupy wide, high; * * @param view */private void Measureview (view view) {Viewgroup.layoutparams 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 height;int tempheight = p.height;if (Tempheight > 0) {Heig HT = Measurespec.makemeasurespec (tempheight,measurespec.exactly);} else {height = measurespec.makemeasurespec (0, measurespec.unspecified);} View.measure (width, height);} /** * Set header layout top margin; * * @param toppadding */private void toppadding (int toppadding) {header.setpadding (Header.getpaddi Ngleft (), Toppadding,header.getpaddingright (), Header.getpaddingbottom ()); Header.invalidate (); @Overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) { This.firstvisibleitem = Firstvisibleitem;} @Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {this.scrollstate = scrollstate;} @SuppressLint ("Clickableviewaccessibility") @Overridepublic boolean ontouchevent (motionevent ev{//TODO auto-generated method Stubswitch (Ev.getaction ()) {case MotionEvent.ACTION_DOWN:if (Firstvisibleitem = 0) {IsR Emark = True;starty = (int) ev.gety ();} Break;case MotionEvent.ACTION_MOVE:onMove (EV); break;case MotionEvent.ACTION_UP:if (state = = Relese) {state = reflashing;//load up-to-date data; Reflashviewbystate (); Ireflashlistener.onreflash ();} else if (state = = pull) {state = None;isremark = False;reflashviewbystate ();} break;} return super.ontouchevent (EV);} /** * Determine the movement process operation; * @param ev */private void OnMove (motionevent ev) {if (!isremark) {return;} int tempy = (int) ev.gety (); int space = Tempy-starty;int toppadding = Space-headerheight;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 < Headerheight +) {state = Pull;reflashviewbystate ();} ELSE if (space <= 0) {state = None;isremark = False;reflashviewbystate ();} Break;}} /** * Changes the interface display according to the current state, */private void Reflashviewbystate () {TextView tip = (TextView) Header.findviewbyid (r.id.tip); I Mageview arrow = (ImageView) Header.findviewbyid (R.id.arrow); ProgressBar progress = (ProgressBar) Header.findviewbyid (r.id.progress); Rotateanimation anim = new Rotateanimation (0, 180,rotateanimation.relative_to_self, 0.5f,rotateanimation.relative_to _self, 0.5f); anim.setduration (+); Anim.setfillafter (true); Rotateanimation anim1 = new Rotateanimation (0,rotateanimation.relative_to_self, 0.5f,rotateanimation.relative_ To_self, 0.5f); anim1.setduration (+); Anim1.setfillafter (true); switch (state) {case NONE:arrow.clearAnimation (); TopPadding (-headerheight); Break;case PULL:arrow.setVisibility (view.visible);p rogress.setvisibility (View.gone); Tip.settext ("drop-down can be refreshed!") "); Arrow.clearanimation (); arrow.setanimation (ANIM1); Break;case RELESE:arrow.setVisibility (view.visible); Progress.setvisibility (View.gone); Tip.settext ("Release can be refreshed! "); Arrow.clearanimation (); arrow.setanimation (anim); break;case reflashing:toppadding (); Arrow.setvisibility ( View.gone);p rogress.setvisibility (view.visible); Tip.settext ("refreshing ..."); Arrow.clearanimation (); break;}} /** * get finished data; */public void Reflashcomplete () {state = None;isremark = False;reflashviewbystate (); TextView lastupdatetime = (TextView) Header.findviewbyid (r.id.freshtime); SimpleDateFormat format = new SimpleDateFormat ("yyyy mm month DD Day HH:MM:SS");D ate date = new Date (System.currenttimemillis ()) ; String time = Format.format (date); Lastupdatetime.settext (time);} public void Setinterface (Ireflashlistener ireflashlistener) {this.ireflashlistener = Ireflashlistener;} /** * Refresh Data Interface * * @author Administrator */public interface Ireflashlistener {public void Onreflash ();}}

Implementing the test class code that refreshes the page

Package Com.example.listviewfreshdemo1;import Java.util.arraylist;import Java.util.list;import Com.example.listviewfreshdemo1.reflashlistview.ireflashlistener;import Android.os.bundle;import Android.os.handler;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class Fragmentpk_1 extends Fragment implements Ireflashlistener {private View view;private reflashlistview mylistview;private myadapter myadapter;private List< user> users; @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {view = Inflater.inflate (R.layout.pk_1, container, false); SetData (); Showlist (); return view;} /** * Notification interface displays */private void Showlist () {if (Myadapter = = null) {Mylistview = (Reflashlistview) View.findviewbyid (R.id.flas Hview); myadapter = new Myadapter (getactivity (), users, Mylistview); Mylistview.setadapter (Myadapter); Mylistview.setinterface (this);} else {Myadapter.datachangEd (users);}} /** * Set data */private void SetData () {users = new arraylist<user> (); for (int i = 0; i < i++) {User user = new User (); User.setname ("Old big:" + i); User.setinfo ("Pit Daddy:" + i); Users.add (user);} /** * Set refreshed data */private void Setreflashdata () {for (int i = 0; i < 2; i++) {User user = new User (); User.setname ("This is rebirth Old big: "+ i"; User.setinfo ("Continue pit Daddy:" + i); Users.add (0, user);}} @Overridepublic void Onreflash () {Handler Handler = new Handler (); handler.postdelayed (new Runnable () {@Overridepublic void Run () {//Get Latest data setreflashdata ();//Notification interface displays showlist ();//Notification listview Refresh Data complete; Mylistview.reflashcomplete ();}}, 2000) ;}}

The demo download page for this example (0-point download, and the increase indicator changes depending on the page)

http://download.csdn.net/detail/a296777513/8346699

Android-based pedometer (pedometer) explanation (vi)--listview dropdown Refresh Page

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.