The pull-down refresh function similar to Sina Weibo is used to view the latest news! Display the latest message at the top!
The Code is as follows:
Code of the pulltorefreshlistview class
Package COM. markupartist. android. widget; import Java. util. date; import COM. markupartist. android. example. pulltorefresh. r; 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. linearinterpolator; import android. view. anim Ation. rotateanimation; import android. widget. abslistview; import android. widget. abslistview. onscrolllistener; import android. widget. baseadapter; import android. widget. imageview; import android. widget. linearlayout; import android. widget. listview; import android. widget. progressbar; import android. widget. textview; public class pulltorefreshlistview 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; // ratio of actual padding distance to the offset on the Interface private final static int ratio = 3; private layoutinflater Inflater; private linearlayout headview; private textview tipstextview; private text View lastupdatedtextview; private imageview arrowimageview; private progressbar; private region animation; private rotateanimation reverseanimation; // This ensures that the value of starty is recorded only once in a complete touch event; private int headcontentwidth; private int headcontentheight; private int starty; private int firstitemindex; private int state; private Boolean isback; private onrefreshlistener re Freshlistener; private Boolean isrefreshable; Public pulltorefreshlistview (context) {super (context); Init (context);} public pulltorefreshlistview (context, attributeset attrs) {super (context, context, attrs); Init (context);} private void Init (context) {setcachecolorhint (context. getresources (). getcolor (Android. r. color. transparent); Inflater = layoutinflater. from (context); headview = (linearl Ayout) Inflater. inflate (R. layout. pull_to_refresh_header, 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. fi Ndviewbyid (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 rotateanima Tion (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_to_self, 0.5f); reverseanimation. setinterpolator (New linearinterpolator (); reverseanimatio N. setduration (200); reverseanimation. setfillafter (true); State = done; isrefreshable = false;} public void onscroll (abslistview arg0, int firstvisiableitem, int arg2, int arg3) {firstitemindex = firstvisiableitem ;} public void onscrollstatechanged (abslistview arg0, int arg1) {} public Boolean ontouchevent (motionevent event) {firstitemindex = getfirstvisibleposition (); If (isrefreshable) {Switch (event. getac Tion () {Case motionevent. action_down: If (firstitemindex = 0 &&! Isrecored) {isrecored = true; starty = (INT) event. gety (); log. V (TAG, "record current position when down '");} break; Case motionevent. action_up: If (State! = Refreshing & state! = Loading) {If (State = done) {// nothing else} If (State = pull_to_refresh) {state = done; changeheaderviewbystate (); log. V (TAG, "Refresh from drop-down to done status");} If (State = release_to_refresh) {state = refreshing; changeheaderviewbystate (); onrefresh (); log. V (TAG, "from release refresh status to done status") ;}} isrecored = false; isback = false; break; Case motionevent. action_move: int Tempy = (INT) event. gety (); If (! Isrecored & firstitemindex = 0) {log. V (TAG, "record location when moving"); isrecored = true; starty = Tempy;} If (State! = Refreshing & isrecored & state! = Loading) {// ensure that the current position is always in the head during the padding setting process. Otherwise, if the list exceeds the screen, when pushing, the list will scroll at the same time. // you can easily refresh the IF (State = release_to_refresh) {setselection (0); // push up to the extent that the screen is sufficient to cover up the head, however, it has not been pushed to the full-covered level. If (Tempy-starty)/ratio
Activity call code
package com.markupartist.android.example.pulltorefresh;import java.util.Arrays;import java.util.LinkedList;import android.app.ListActivity;import android.os.AsyncTask;import android.os.Bundle;import android.widget.ArrayAdapter;import com.markupartist.android.widget.PullToRefreshListView;import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;public class PullToRefreshActivity extends ListActivity { private LinkedList<String> mListItems; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pull_to_refresh); // Set a listener to be invoked when the list should be refreshed. ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { // Do work to refresh the list here. new GetDataTask().execute(); } }); mListItems = new LinkedList<String>(); mListItems.addAll(Arrays.asList(mStrings)); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); setListAdapter(adapter); } private class GetDataTask extends AsyncTask<Void, Void, String[]> { @Override protected String[] doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(2000); } catch (InterruptedException e) { ; } return mStrings; } @Override protected void onPostExecute(String[] result) { mListItems.addFirst("Added after refresh..."); // Call onRefreshComplete when the list has been refreshed. ((PullToRefreshListView) getListView()).onRefreshComplete(); super.onPostExecute(result); } } private String[] mStrings = {// "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",// "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler"};}