`
andy_叶
  • 浏览: 67994 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

android之OnScrollListener 下拉刷新

 
阅读更多

1.首先创建一个头部xml文件 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <!-- ListView的头部 -->  
  4. <LinearLayout  
  5.   xmlns:android="http://schemas.android.com/apk/res/android"  
  6.   android:layout_width="fill_parent"  
  7.   android:layout_height="wrap_content"  
  8.  >  
  9.     
  10.   <!-- 内容 -->  
  11.   <RelativeLayout  
  12.   android:layout_width="fill_parent"  
  13.   android:layout_height="wrap_content"  
  14.   android:id="@+id/head_contentLayout"  
  15.   android:paddingLeft="30dp"  
  16.   >  
  17.     
  18.   <!-- 箭头图像、进度条 -->  
  19.   <FrameLayout  
  20.   android:layout_width="wrap_content"  
  21.   android:layout_height="wrap_content"  
  22.   android:layout_alignParentLeft="true"  
  23.   android:layout_centerVertical="true"  
  24.   >  
  25.     
  26.   <!-- 箭头 -->  
  27.   <ImageView  
  28.   android:layout_width="wrap_content"  
  29.   android:layout_height="wrap_content"  
  30.   android:layout_gravity="center"  
  31.   android:src="@drawable/pull_down_arrow"  
  32.   android:id="@+id/head_arrowImageView"  
  33.   />  
  34.     
  35.   <!-- 进度条 -->  
  36.   <ProgressBar  
  37.   android:layout_width="wrap_content"  
  38.   android:layout_height="wrap_content"  
  39.   style="?android:attr/progressBarStyleSmall"  
  40.   android:layout_gravity="center"  
  41.   android:id="@+id/head_progressBar"  
  42.     
  43.   android:visibility="gone"  
  44.   />  
  45.     
  46.   </FrameLayout>  
  47.     
  48.   <!-- 提示、最近更新 -->  
  49.   <LinearLayout  
  50.   android:layout_width="wrap_content"  
  51.   android:layout_height="wrap_content"  
  52.   android:layout_centerHorizontal="true"  
  53.   android:orientation="vertical"  
  54.   android:gravity="center_horizontal"  
  55.   >  
  56.     
  57.   <!-- 提示 -->  
  58.   <TextView  
  59.   android:layout_width="wrap_content"  
  60.   android:layout_height="wrap_content"  
  61.   android:text="下拉刷新"  
  62.   android:textColor="@color/white"  
  63.   android:textSize="20sp"  
  64.   android:id="@+id/head_tipsTextView"  
  65.   />  
  66.     
  67.   <!-- 最近更新 -->  
  68.   <TextView  
  69.   android:layout_width="wrap_content"  
  70.   android:layout_height="wrap_content"  
  71.   android:id="@+id/head_lastUpdatedTextView"  
  72.   android:text="上次更新"  
  73.   android:textColor="@color/gold"  
  74.   android:textSize="10sp"  
  75.   />  
  76.     
  77.   </LinearLayout>  
  78.     
  79.     
  80.   </RelativeLayout>  
  81.     
  82.     
  83. </LinearLayout>  

2.然后写一个class: 

Java代码  收藏代码
  1. package com.laohuai.appdemo.customui.ui;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import com.laohuai.appdemo.customui.R;  
  6.   
  7. import android.content.Context;  
  8. import android.util.AttributeSet;  
  9. import android.util.Log;  
  10. import android.view.LayoutInflater;  
  11. import android.view.MotionEvent;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.view.animation.LinearInterpolator;  
  15. import android.view.animation.RotateAnimation;  
  16. import android.widget.AbsListView;  
  17. import android.widget.BaseAdapter;  
  18. import android.widget.ImageView;  
  19. import android.widget.LinearLayout;  
  20. import android.widget.ListView;  
  21. import android.widget.AbsListView.OnScrollListener;  
  22. import android.widget.ProgressBar;  
  23. import android.widget.TextView;  
  24.   
  25. public class MyListView extends ListView implements OnScrollListener {  
  26.   
  27.     private static final String TAG = "listview";  
  28.   
  29.     private final static int RELEASE_To_REFRESH = 0;  
  30.     private final static int PULL_To_REFRESH = 1;  
  31.     private final static int REFRESHING = 2;  
  32.     private final static int DONE = 3;  
  33.     private final static int LOADING = 4;  
  34.   
  35.     // 实际的padding的距离与界面上偏移距离的比例  
  36.     private final static int RATIO = 3;  
  37.   
  38.     private LayoutInflater inflater;  
  39.   
  40.     private LinearLayout headView;  
  41.   
  42.     private TextView tipsTextview;  
  43.     private TextView lastUpdatedTextView;  
  44.     private ImageView arrowImageView;  
  45.     private ProgressBar progressBar;  
  46.   
  47.   
  48.     private RotateAnimation animation;  
  49.     private RotateAnimation reverseAnimation;  
  50.   
  51.     // 用于保证startY的值在一个完整的touch事件中只被记录一次  
  52.     private boolean isRecored;  
  53.   
  54.     private int headContentWidth;  
  55.     private int headContentHeight;  
  56.   
  57.     private int startY;  
  58.     private int firstItemIndex;  
  59.   
  60.     private int state;  
  61.   
  62.     private boolean isBack;  
  63.   
  64.     private OnRefreshListener refreshListener;  
  65.   
  66.     private boolean isRefreshable;  
  67.   
  68.     public MyListView(Context context) {  
  69.         super(context);  
  70.         init(context);  
  71.     }  
  72.   
  73.     public MyListView(Context context, AttributeSet attrs) {  
  74.         super(context, attrs);  
  75.         init(context);  
  76.     }  
  77.   
  78.     private void init(Context context) {  
  79.         setCacheColorHint(context.getResources().getColor(R.color.transparent));  
  80.         inflater = LayoutInflater.from(context);  
  81.   
  82.         headView = (LinearLayout) inflater.inflate(R.layout.head, null);  
  83.   
  84.         arrowImageView = (ImageView) headView  
  85.                 .findViewById(R.id.head_arrowImageView);  
  86.         arrowImageView.setMinimumWidth(70);  
  87.         arrowImageView.setMinimumHeight(50);  
  88.         progressBar = (ProgressBar) headView  
  89.                 .findViewById(R.id.head_progressBar);  
  90.         tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);  
  91.         lastUpdatedTextView = (TextView) headView  
  92.                 .findViewById(R.id.head_lastUpdatedTextView);  
  93.   
  94.         measureView(headView);  
  95.         headContentHeight = headView.getMeasuredHeight();  
  96.         headContentWidth = headView.getMeasuredWidth();  
  97.   
  98.         headView.setPadding(0, -1 * headContentHeight, 00);  
  99.         headView.invalidate();  
  100.   
  101.         Log.v("size""width:" + headContentWidth + " height:"  
  102.                 + headContentHeight);  
  103.   
  104.         addHeaderView(headView, nullfalse);  
  105.         setOnScrollListener(this);  
  106.   
  107.         animation = new RotateAnimation(0, -180,  
  108.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  109.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  110.         animation.setInterpolator(new LinearInterpolator());  
  111.         animation.setDuration(250);  
  112.         animation.setFillAfter(true);  
  113.   
  114.         reverseAnimation = new RotateAnimation(-1800,  
  115.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  116.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  117.         reverseAnimation.setInterpolator(new LinearInterpolator());  
  118.         reverseAnimation.setDuration(200);  
  119.         reverseAnimation.setFillAfter(true);  
  120.   
  121.         state = DONE;  
  122.         isRefreshable = false;  
  123.     }  
  124.   
  125.     public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,  
  126.             int arg3) {  
  127.         firstItemIndex = firstVisiableItem;  
  128.     }  
  129.   
  130.     public void onScrollStateChanged(AbsListView arg0, int arg1) {  
  131.     }  
  132.   
  133.     public boolean onTouchEvent(MotionEvent event) {  
  134.   
  135.         if (isRefreshable) {  
  136.             switch (event.getAction()) {  
  137.             case MotionEvent.ACTION_DOWN:  
  138.                 if (firstItemIndex == 0 && !isRecored) {  
  139.                     isRecored = true;  
  140.                     startY = (int) event.getY();  
  141.                     Log.v(TAG, "在down时候记录当前位置‘");  
  142.                 }  
  143.                 break;  
  144.   
  145.             case MotionEvent.ACTION_UP:  
  146.   
  147.                 if (state != REFRESHING && state != LOADING) {  
  148.                     if (state == DONE) {  
  149.                         // 什么都不做  
  150.                     }  
  151.                     if (state == PULL_To_REFRESH) {  
  152.                         state = DONE;  
  153.                         changeHeaderViewByState();  
  154.   
  155.                         Log.v(TAG, "由下拉刷新状态,到done状态");  
  156.                     }  
  157.                     if (state == RELEASE_To_REFRESH) {  
  158.                         state = REFRESHING;  
  159.                         changeHeaderViewByState();  
  160.                         onRefresh();  
  161.   
  162.                         Log.v(TAG, "由松开刷新状态,到done状态");  
  163.                     }  
  164.                 }  
  165.   
  166.                 isRecored = false;  
  167.                 isBack = false;  
  168.   
  169.                 break;  
  170.   
  171.             case MotionEvent.ACTION_MOVE:  
  172.                 int tempY = (int) event.getY();  
  173.   
  174.                 if (!isRecored && firstItemIndex == 0) {  
  175.                     Log.v(TAG, "在move时候记录下位置");  
  176.                     isRecored = true;  
  177.                     startY = tempY;  
  178.                 }  
  179.   
  180.                 if (state != REFRESHING && isRecored && state != LOADING) {  
  181.   
  182.                     // 保证在设置padding的过程中,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动  
  183.   
  184.                     // 可以松手去刷新了  
  185.                     if (state == RELEASE_To_REFRESH) {  
  186.   
  187.                         setSelection(0);  
  188.   
  189.                         // 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步  
  190.                         if (((tempY - startY) / RATIO < headContentHeight)  
  191.                                 && (tempY - startY) > 0) {  
  192.                             state = PULL_To_REFRESH;  
  193.                             changeHeaderViewByState();  
  194.   
  195.                             Log.v(TAG, "由松开刷新状态转变到下拉刷新状态");  
  196.                         }  
  197.                         // 一下子推到顶了  
  198.                         else if (tempY - startY <= 0) {  
  199.                             state = DONE;  
  200.                             changeHeaderViewByState();  
  201.   
  202.                             Log.v(TAG, "由松开刷新状态转变到done状态");  
  203.                         }  
  204.                         // 往下拉了,或者还没有上推到屏幕顶部掩盖head的地步  
  205.                         else {  
  206.                             // 不用进行特别的操作,只用更新paddingTop的值就行了  
  207.                         }  
  208.                     }  
  209.                     // 还没有到达显示松开刷新的时候,DONE或者是PULL_To_REFRESH状态  
  210.                     if (state == PULL_To_REFRESH) {  
  211.   
  212.                         setSelection(0);  
  213.   
  214.                         // 下拉到可以进入RELEASE_TO_REFRESH的状态  
  215.                         if ((tempY - startY) / RATIO >= headContentHeight) {  
  216.                             state = RELEASE_To_REFRESH;  
  217.                             isBack = true;  
  218.                             changeHeaderViewByState();  
  219.   
  220.                             Log.v(TAG, "由done或者下拉刷新状态转变到松开刷新");  
  221.                         }  
  222.                         // 上推到顶了  
  223.                         else if (tempY - startY <= 0) {  
  224.                             state = DONE;  
  225.                             changeHeaderViewByState();  
  226.   
  227.                             Log.v(TAG, "由DOne或者下拉刷新状态转变到done状态");  
  228.                         }  
  229.                     }  
  230.   
  231.                     // done状态下  
  232.                     if (state == DONE) {  
  233.                         if (tempY - startY > 0) {  
  234.                             state = PULL_To_REFRESH;  
  235.                             changeHeaderViewByState();  
  236.                         }  
  237.                     }  
  238.   
  239.                     // 更新headView的size  
  240.                     if (state == PULL_To_REFRESH) {  
  241.                         headView.setPadding(0, -1 * headContentHeight  
  242.                                 + (tempY - startY) / RATIO, 00);  
  243.   
  244.                     }  
  245.   
  246.                     // 更新headView的paddingTop  
  247.                     if (state == RELEASE_To_REFRESH) {  
  248.                         headView.setPadding(0, (tempY - startY) / RATIO  
  249.                                 - headContentHeight, 00);  
  250.                     }  
  251.   
  252.                 }  
  253.   
  254.                 break;  
  255.             }  
  256.         }  
  257.   
  258.         return super.onTouchEvent(event);  
  259.     }  
  260.   
  261.     // 当状态改变时候,调用该方法,以更新界面  
  262.     private void changeHeaderViewByState() {  
  263.         switch (state) {  
  264.         case RELEASE_To_REFRESH:  
  265.             arrowImageView.setVisibility(View.VISIBLE);  
  266.             progressBar.setVisibility(View.GONE);  
  267.             tipsTextview.setVisibility(View.VISIBLE);  
  268.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  269.   
  270.             arrowImageView.clearAnimation();  
  271.             arrowImageView.startAnimation(animation);  
  272.   
  273.             tipsTextview.setText("松开刷新");  
  274.   
  275.             Log.v(TAG, "当前状态,松开刷新");  
  276.             break;  
  277.         case PULL_To_REFRESH:  
  278.             progressBar.setVisibility(View.GONE);  
  279.             tipsTextview.setVisibility(View.VISIBLE);  
  280.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  281.             arrowImageView.clearAnimation();  
  282.             arrowImageView.setVisibility(View.VISIBLE);  
  283.             // 是由RELEASE_To_REFRESH状态转变来的  
  284.             if (isBack) {  
  285.                 isBack = false;  
  286.                 arrowImageView.clearAnimation();  
  287.                 arrowImageView.startAnimation(reverseAnimation);  
  288.   
  289.                 tipsTextview.setText("下拉刷新");  
  290.             } else {  
  291.                 tipsTextview.setText("下拉刷新");  
  292.             }  
  293.             Log.v(TAG, "当前状态,下拉刷新");  
  294.             break;  
  295.   
  296.         case REFRESHING:  
  297.   
  298.             headView.setPadding(0000);  
  299.   
  300.             progressBar.setVisibility(View.VISIBLE);  
  301.             arrowImageView.clearAnimation();  
  302.             arrowImageView.setVisibility(View.GONE);  
  303.             tipsTextview.setText("正在刷新...");  
  304.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  305.   
  306.             Log.v(TAG, "当前状态,正在刷新...");  
  307.             break;  
  308.         case DONE:  
  309.             headView.setPadding(0, -1 * headContentHeight, 00);  
  310.   
  311.             progressBar.setVisibility(View.GONE);  
  312.             arrowImageView.clearAnimation();  
  313.             arrowImageView.setImageResource(R.drawable.pull_down_arrow);  
  314.             tipsTextview.setText("下拉刷新");  
  315.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  316.   
  317.             Log.v(TAG, "当前状态,done");  
  318.             break;  
  319.         }  
  320.     }  
  321.   
  322.     public void setonRefreshListener(OnRefreshListener refreshListener) {  
  323.         this.refreshListener = refreshListener;  
  324.         isRefreshable = true;  
  325.     }  
  326.   
  327.     public interface OnRefreshListener {  
  328.         public void onRefresh();  
  329.     }  
  330.   
  331.     public void onRefreshComplete() {  
  332.         state = DONE;  
  333.         lastUpdatedTextView.setText("最近更新:" + new Date().toLocaleString());  
  334.         changeHeaderViewByState();  
  335.     }  
  336.   
  337.     private void onRefresh() {  
  338.         if (refreshListener != null) {  
  339.             refreshListener.onRefresh();  
  340.         }  
  341.     }  
  342.   
  343.     // 此方法直接照搬自网络上的一个下拉刷新的demo,此处是“估计”headView的width以及height  
  344.     private void measureView(View child) {  
  345.         ViewGroup.LayoutParams p = child.getLayoutParams();  
  346.         if (p == null) {  
  347.             p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,  
  348.                     ViewGroup.LayoutParams.WRAP_CONTENT);  
  349.         }  
  350.         int childWidthSpec = ViewGroup.getChildMeasureSpec(00 + 0, p.width);  
  351.         int lpHeight = p.height;  
  352.         int childHeightSpec;  
  353.         if (lpHeight > 0) {  
  354.             childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,  
  355.                     MeasureSpec.EXACTLY);  
  356.         } else {  
  357.             childHeightSpec = MeasureSpec.makeMeasureSpec(0,  
  358.                     MeasureSpec.UNSPECIFIED);  
  359.         }  
  360.         child.measure(childWidthSpec, childHeightSpec);  
  361.     }  
  362.   
  363.     public void setAdapter(BaseAdapter adapter) {  
  364.         lastUpdatedTextView.setText("最近更新:" + new Date().toLocaleString());  
  365.         super.setAdapter(adapter);  
  366.     }  
  367.   
  368. }  

3.在main.xml 中调用上面的这个class: 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="#FFFFFF">  
  7.       
  8.     <com.laohuai.appdemo.customui.ui.MyListView  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="fill_parent"  
  11.     android:id="@+id/listView"  
  12.     android:listSelector="@android:color/transparent"  
  13.     />  
  14.       
  15. </LinearLayout>  


4.实现Activity: 

Java代码  收藏代码
  1. package com.laohuai.appdemo.customui;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import com.laohuai.appdemo.customui.ui.MyListView;  
  8. import com.laohuai.appdemo.customui.ui.MyListView.OnRefreshListener;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.Context;  
  12. import android.content.res.Resources;  
  13. import android.os.AsyncTask;  
  14. import android.os.Bundle;  
  15. import android.view.LayoutInflater;  
  16. import android.view.View;  
  17. import android.view.ViewGroup;  
  18. import android.widget.BaseAdapter;  
  19. import android.widget.TextView;  
  20.   
  21. public class MainActivity extends Activity {  
  22.     HashMap<String, Object> maps;  
  23.     MyListView listView;  
  24.   
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.         listView = (MyListView) findViewById(R.id.listView);  
  29.         getData();  
  30.     }  
  31.   
  32.     private void getData() {  
  33.         List<HashMap<String, Object>> lists = new ArrayList<HashMap<String, Object>>();  
  34.   
  35.         for (int i = 0; i < 40; i++) {  
  36.   
  37.             maps = new HashMap<String, Object>();  
  38.             maps.put("test""搞定了没有啊");  
  39.   
  40.             lists.add(maps);  
  41.         }  
  42.         final ListAdapter adapter = new ListAdapter(MainActivity.this, lists);  
  43.         listView.setAdapter(adapter);  
  44.   
  45.         listView.setonRefreshListener(new OnRefreshListener() {  
  46.             public void onRefresh() {  
  47.                 new AsyncTask<Void, Void, Void>() {  
  48.                     protected Void doInBackground(Void... params) {  
  49.                         try {  
  50.                             Thread.sleep(1000);  
  51.                         } catch (Exception e) {  
  52.                             e.printStackTrace();  
  53.                         }  
  54.                         maps.get("刷新后添加的内容");  
  55.                         return null;  
  56.                     }  
  57.   
  58.                     protected void onPostExecute(Void result) {  
  59.                         adapter.notifyDataSetChanged();  
  60.                         listView.onRefreshComplete();  
  61.                     }  
  62.   
  63.                 }.execute(null);  
  64.             }  
  65.         });  
  66.     }  
  67.   
  68.     class ListAdapter extends BaseAdapter {  
  69.   
  70.         private Context mContext;  
  71.         private List<HashMap<String, Object>> data;  
  72.   
  73.         public ListAdapter(Context mContext, List<HashMap<String, Object>> data) {  
  74.             super();  
  75.             this.mContext = mContext;  
  76.             this.data = data;  
  77.         }  
  78.   
  79.         public int getCount() {  
  80.             // TODO Auto-generated method stub  
  81.             return data.size();  
  82.         }  
  83.   
  84.         public Object getItem(int position) {  
  85.             // TODO Auto-generated method stub  
  86.             return position;  
  87.         }  
  88.   
  89.         public long getItemId(int position) {  
  90.             // TODO Auto-generated method stub  
  91.             return position;  
  92.         }  
  93.   
  94.         public View getView(int position, View convertView, ViewGroup parent) {  
  95.             TextView tv = new TextView(getApplicationContext());  
  96.             tv.setText((String)data.get(position).get("test"));  
  97.             Resources rs = getResources();  
  98.             tv.setTextColor(rs.getColor(R.color.white));  
  99.             return tv;  
  100.         }  
  101.   
  102.     }  
  103. }  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics