Android-ListView pull-down refresh

Source: Internet
Author: User

Android-ListView pull-down refresh

 

Share a pull-down refresh solution ,:

 

Main. java:

 

package example.com.list;import java.util.LinkedList;import example.com.list.R;import example.com.list.R.id;import example.com.list.R.layout;import example.com.list.MyListView.OnRefreshListener;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class MainActivity extends Activity {private LinkedList
 
   data;private BaseAdapter adapter;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);data = new LinkedList
  
   ();    for(int i=0;i<6;i++){    data.add(String.valueOf(i));    }final MyListView listView = (MyListView) findViewById(R.id.listView);adapter = new BaseAdapter() {public View getView(int position, View convertView, ViewGroup parent) { convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);TextView textView = (TextView) convertView.findViewById(R.id.textView_item);textView.setText(data.get(position));return convertView;}public long getItemId(int position) {return position;}public Object getItem(int position) {return data.get(position);}public int getCount() {return data.size();}};listView.setAdapter(adapter);listView.setonRefreshListener(new OnRefreshListener() {public void onRefresh() {new AsyncTask
   
    () {protected Void doInBackground(Void... params) {try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}return null;}@Overrideprotected void onPostExecute(Void result) {adapter.notifyDataSetChanged();listView.onRefreshComplete();}};}});}}
   
  
 
ListView. java:

 

 

Package example.com. list; import java. text. simpleDateFormat; import java. util. date; import example.com. list. r; import example.com. list. r. drawable; import example.com. list. r. id; import example.com. list. r. layout; 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. vi Ew. viewGroup; import android. view. animation. linearInterpolator; import android. view. animation. rotateAnimation; import android. widget. absListView; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. linearLayout; import android. widget. listView; import android. widget. absListView. onScrollListener; import android. widget. progressBar; import android. widget. textView; public CIA Ss MyListView extends ListView implements OnScrollListener {private static final String TAG = listview; private final static int RELEASE_To_REFRESH = 0; // release refresh private final static int PULL_To_REFRESH = 1; // pull and refresh private final static int REFRESHING = 2; // REFRESHING private final static int DONE = 3; private final static int LOADING = 4; // RATIO of actual padding distance to interface offset private final static int RATIO = 3; private LayoutInflater inflater; private LinearLayout headView; private TextView tipsTextview; private TextView lastUpdatedTextView; private ImageView arrowImageView; private ProgressBar progressBar; private implements animation; private implements reverseAnimation; // ensure that the value of startY is recorded only once in a complete touch event. private int headContentWidth; private int headContentHeight; private int start Y; private int firstItemIndex; private int state; private boolean isBack; private OnRefreshListener refreshListener; private boolean isRefreshable; public MyListView (Context context) {super (context); init (context );} public MyListView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} private void init (Context context) {// setCacheColorHint (context. getResources (). getColor (R. color. tra Nsparent); inflater = LayoutInflater. from (context); headView = (LinearLayout) inflater. inflate (R. layout. head, 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_tipsTe XtView); lastUpdatedTextView = (TextView) headView. findViewById (R. id. head_lastUpdatedTextView); measureView (headView); headContentHeight = headView. getMeasuredHeight (); headContentWidth = headView. getMeasuredWidth (); headView. setPadding (0,-1 * headContentHeight, 0, 0); headView. invalidate (); addHeaderView (headView, null, false); setOnScrollListener (this); animation = new RotateAnimation (0,-180, RotateAnim Ation. 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 (); reverseAnimation. setDuration (1, 200); reve RseAnimation. 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) {if (isRefreshable) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: if (firstItemIndex = 0 &&! IsRecored) {isRecored = true; startY = (int) event. getY ();} break; case MotionEvent. ACTION_UP: if (state! = REFRESHING & state! = LOADING) {if (state = DONE) {} if (state = PULL_To_REFRESH) {state = DONE; changeHeaderViewByState ();} if (state = RELEASE_To_REFRESH) {state = REFRESHING; changeHeaderViewByState (); onRefresh () ;}} isRecored = false; isBack = false; break; case MotionEvent. ACTION_MOVE: int tempY = (int) event. getY (); if (! IsRecored & firstItemIndex = 0) {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 

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.