Pull to refresh implemention

来源:互联网 发布:dota三大后期 知乎 编辑:程序博客网 时间:2024/04/28 11:20

pull to refresh  源于IPhone,现在很多项目都有实现,在github 有个开源的Pull To Refresh 项目,看过感觉代码太重了,日常项目中要是用它有点杀鸡用牛刀大才小用了

其实网上早已有了它简易的实现,看代码:



该控件特点:

1.子控件必须是一个ScrollView或ListView;

2.支持自定义下拉布局;

3.自定义下拉布局可以不用处理下拉的各种状态(只需要实现几个接口即可),也可以自己处理各种下拉的状态。

 

先来看看效果图:



 



 



 

上代码:

首先看如何使用:

1.使用的布局:

Java代码  收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    xmlns:tools="http://schemas.android.com/tools"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:orientation="vertical" >  
  6.   
  7.    <com.example.pulldown.PullDownScrollView  
  8.        android:id="@+id/refresh_root"  
  9.        android:layout_width="fill_parent"  
  10.        android:layout_height="0dip"  
  11.        android:layout_weight="1"  
  12.        android:background="#161616"  
  13.        android:orientation="vertical" >  
  14.   
  15.        <ScrollView  
  16.            android:id="@+id/scrollview"  
  17.            android:layout_width="fill_parent"  
  18.            android:layout_height="wrap_content"  
  19.            android:scrollbars="none" >  
  20.   
  21.            <LinearLayout  
  22.                android:id="@+id/mainView"  
  23.                android:layout_width="fill_parent"  
  24.                android:layout_height="wrap_content"  
  25.                android:background="#1f1f1f"  
  26.                android:orientation="vertical" >  
  27.   
  28.                <!-- 自已的布局 -->  
  29.   
  30.                <TextView  
  31.                    android:layout_width="match_parent"  
  32.                    android:layout_height="wrap_content"  
  33.                    android:layout_marginTop="10dip"  
  34.                    android:gravity="center"  
  35.                    android:text="@string/hello_world"  
  36.                    android:textColor="@android:color/white"  
  37.                    android:textSize="18sp" />  
  38.   
  39.                <TextView  
  40.                    android:layout_width="match_parent"  
  41.                    android:layout_height="wrap_content"  
  42.                    android:layout_marginTop="10dip"  
  43.                    android:gravity="center"  
  44.                    android:text="@string/hello_world"  
  45.                    android:textColor="@android:color/white"  
  46.                    android:textSize="18sp" />  
  47.   
  48.                <TextView  
  49.                    android:layout_width="match_parent"  
  50.                    android:layout_height="wrap_content"  
  51.                    android:layout_marginTop="10dip"  
  52.                    android:gravity="center"  
  53.                    android:text="@string/hello_world"  
  54.                    android:textColor="@android:color/white"  
  55.                    android:textSize="18sp" />  
  56.   
  57.                <TextView  
  58.                    android:layout_width="match_parent"  
  59.                    android:layout_height="wrap_content"  
  60.                    android:layout_marginTop="10dip"  
  61.                    android:gravity="center"  
  62.                    android:text="@string/hello_world"  
  63.                    android:textColor="@android:color/white"  
  64.                    android:textSize="18sp" />  
  65.            </LinearLayout>  
  66.        </ScrollView>  
  67.    </com.example.pulldown.PullDownScrollView>  
  68.   
  69. lt;/LinearLayout>  

 2.UI使用:

   首先,Activity实现接口:

implements RefreshListener

 部分代码如下:

Java代码  收藏代码
  1.   package com.example.pulldown;  
  2.   
  3. import com.example.pulldown.PullDownScrollView.RefreshListener;  
  4.   
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.app.Activity;  
  8. import android.view.Menu;  
  9.   
  10. public class MainActivity extends Activity implements RefreshListener{  
  11.     private PullDownScrollView mPullDownScrollView;   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.           
  17.         mPullDownScrollView = (PullDownScrollView) findViewById(R.id.refresh_root);    
  18.         mPullDownScrollView.setRefreshListener(this);    
  19.         mPullDownScrollView.setPullDownElastic(new PullDownElasticImp(this));    
  20.     }  
  21.     @Override    
  22.     public void onRefresh(PullDownScrollView view) {    
  23.         new Handler().postDelayed(new Runnable() {    
  24.                 
  25.             @Override    
  26.             public void run() {    
  27.                 // TODO Auto-generated method stub    
  28.                 mPullDownScrollView.finishRefresh("上次刷新时间:12:23");    
  29.             }    
  30.         }, 2000);    
  31.     }    
  32.     @Override  
  33.     public boolean onCreateOptionsMenu(Menu menu) {  
  34.         // Inflate the menu; this adds items to the action bar if it is present.  
  35.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  36.         return true;  
  37.     }  
  38.   
  39. }  
 

3.再来看看控件代码:

Java代码  收藏代码
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.view.MotionEvent;  
  4. import android.view.View;  
  5. import android.view.animation.LinearInterpolator;  
  6. import android.view.animation.RotateAnimation;  
  7. import android.widget.AbsListView;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.ScrollView;  
  10.   
  11.   
  12. /** 
  13.  * @author xwangly@163.com 
  14.  * @date 2013-7-9 
  15.  *  
  16.  */  
  17. public class PullDownScrollView extends LinearLayout {  
  18.   
  19.     private static final String TAG = "PullDownScrollView";  
  20.   
  21.     private int refreshTargetTop = -60;  
  22.     private int headContentHeight;  
  23.   
  24.     private RefreshListener refreshListener;  
  25.   
  26.     private RotateAnimation animation;  
  27.     private RotateAnimation reverseAnimation;  
  28.       
  29.     private final static int RATIO = 2;  
  30.     private int preY = 0;  
  31.     private boolean isElastic = false;  
  32.     private int startY;  
  33.     private int state;  
  34.       
  35.     private String note_release_to_refresh = "松开更新";  
  36.     private String note_pull_to_refresh = "下拉刷新";  
  37.     private String note_refreshing = "正在更新...";  
  38.       
  39.     private IPullDownElastic mElastic;  
  40.       
  41.   
  42.     public PullDownScrollView(Context context) {  
  43.         super(context);  
  44.         init();  
  45.   
  46.     }  
  47.   
  48.     public PullDownScrollView(Context context, AttributeSet attrs) {  
  49.         super(context, attrs);  
  50.         init();  
  51.     }  
  52.   
  53.     private void init() {  
  54.         animation = new RotateAnimation(0, -180,  
  55.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  56.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  57.         animation.setInterpolator(new LinearInterpolator());  
  58.         animation.setDuration(250);  
  59.         animation.setFillAfter(true);  
  60.   
  61.         reverseAnimation = new RotateAnimation(-1800,  
  62.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  63.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  64.         reverseAnimation.setInterpolator(new LinearInterpolator());  
  65.         reverseAnimation.setDuration(200);  
  66.         reverseAnimation.setFillAfter(true);  
  67.     }  
  68.     /** 
  69.      * 刷新监听 
  70.      * @param listener 
  71.      */  
  72.     public void setRefreshListener(RefreshListener listener) {  
  73.         this.refreshListener = listener;  
  74.     }  
  75.     /** 
  76.      * 下拉布局 
  77.      * @param elastic 
  78.      */  
  79.     public void setPullDownElastic(IPullDownElastic elastic) {  
  80.         mElastic = elastic;  
  81.           
  82.         headContentHeight = mElastic.getElasticHeight();  
  83.         refreshTargetTop = - headContentHeight;  
  84.         LayoutParams lp = new LinearLayout.LayoutParams(  
  85.                 LayoutParams.FILL_PARENT, headContentHeight);  
  86.         lp.topMargin = refreshTargetTop;  
  87.         addView(mElastic.getElasticLayout(), 0, lp);  
  88.     }  
  89.       
  90.     /** 
  91.      * 设置更新提示语 
  92.      * @param pullToRefresh 下拉刷新提示语 
  93.      * @param releaseToRefresh 松开刷新提示语 
  94.      * @param refreshing 正在刷新提示语 
  95.      */  
  96.     public void setRefreshTips(String pullToRefresh, String releaseToRefresh, String refreshing) {  
  97.         note_pull_to_refresh = pullToRefresh;  
  98.         note_release_to_refresh = releaseToRefresh;  
  99.         note_refreshing = refreshing;  
  100.     }  
  101.     /* 
  102.      * 该方法一般和ontouchEvent 一起用 (non-Javadoc) 
  103.      *  
  104.      * @see 
  105.      * android.view.ViewGroup#onInterceptTouchEvent(android.view.MotionEvent) 
  106.      */  
  107.     @Override  
  108.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  109.         Logger.d(TAG, "onInterceptTouchEvent");  
  110.         printMotionEvent(ev);  
  111.         if (ev.getAction() == MotionEvent.ACTION_DOWN) {  
  112.             preY = (int) ev.getY();  
  113.         }  
  114.         if (ev.getAction() == MotionEvent.ACTION_MOVE) {  
  115.   
  116.             Logger.d(TAG, "isElastic:" + isElastic + " canScroll:"+ canScroll() + " ev.getY() - preY:"+(ev.getY() - preY));  
  117.             if (!isElastic && canScroll()  
  118.                     && (int) ev.getY() - preY >= headContentHeight / (3*RATIO)  
  119.                     && refreshListener != null && mElastic != null) {  
  120.   
  121.                 isElastic = true;  
  122.                 startY = (int) ev.getY();  
  123.                 Logger.i(TAG, "在move时候记录下位置startY:" + startY);  
  124.                 return true;  
  125.             }  
  126.   
  127.         }  
  128.         return super.onInterceptTouchEvent(ev);  
  129.     }  
  130.   
  131.     @Override  
  132.     public boolean onTouchEvent(MotionEvent event) {  
  133.         Logger.d(TAG, "onTouchEvent");  
  134.         printMotionEvent(event);  
  135.         handleHeadElastic(event);  
  136.         return super.onTouchEvent(event);  
  137.     }  
  138.   
  139.     private void handleHeadElastic(MotionEvent event) {  
  140.         if (refreshListener != null && mElastic != null) {  
  141.             switch (event.getAction()) {  
  142.             case MotionEvent.ACTION_DOWN:  
  143.                 Logger.i(TAG, "down");  
  144.                 break;  
  145.             case MotionEvent.ACTION_UP:  
  146.   
  147.                 Logger.i(TAG, "up");  
  148.                 if (state != IPullDownElastic.REFRESHING && isElastic) {  
  149.                       
  150.                     if (state == IPullDownElastic.DONE) {  
  151.                         // 什么都不做  
  152.                         setMargin(refreshTargetTop);  
  153.                     }  
  154.                     if (state == IPullDownElastic.PULL_To_REFRESH) {  
  155.                         state = IPullDownElastic.DONE;  
  156.                         setMargin(refreshTargetTop);  
  157.                         changeHeaderViewByState(state, false);  
  158.                         Logger.i(TAG, "由下拉刷新状态,到done状态");  
  159.                     }  
  160.                     if (state == IPullDownElastic.RELEASE_To_REFRESH) {  
  161.                         state = IPullDownElastic.REFRESHING;  
  162.                         setMargin(0);  
  163.                         changeHeaderViewByState(state, false);  
  164.                         onRefresh();  
  165.                         Logger.i(TAG, "由松开刷新状态,到done状态");  
  166.                     }  
  167.   
  168.                 }  
  169.                 isElastic = false;  
  170.                 break;  
  171.             case MotionEvent.ACTION_MOVE:  
  172.                 Logger.i(TAG, "move");  
  173.                 int tempY = (int) event.getY();  
  174.                   
  175.                 if (state != IPullDownElastic.REFRESHING && isElastic) {  
  176.                     // 可以松手去刷新了  
  177.                     if (state == IPullDownElastic.RELEASE_To_REFRESH) {  
  178.                         if (((tempY - startY) / RATIO < headContentHeight)  
  179.                                 && (tempY - startY) > 0) {  
  180.                             state = IPullDownElastic.PULL_To_REFRESH;  
  181.                             changeHeaderViewByState(state, true);  
  182.                             Logger.i(TAG, "由松开刷新状态转变到下拉刷新状态");  
  183.                         } else if (tempY - startY <= 0) {  
  184.                             state = IPullDownElastic.DONE;  
  185.                             changeHeaderViewByState(state, false);  
  186.                             Logger.i(TAG, "由松开刷新状态转变到done状态");  
  187.                         }  
  188.                     }  
  189.                     if (state == IPullDownElastic.DONE) {  
  190.                         if (tempY - startY > 0) {  
  191.                             state = IPullDownElastic.PULL_To_REFRESH;  
  192.                             changeHeaderViewByState(state, false);  
  193.                         }  
  194.                     }  
  195.                     if (state == IPullDownElastic.PULL_To_REFRESH) {  
  196.                         // 下拉到可以进入RELEASE_TO_REFRESH的状态  
  197.                         if ((tempY - startY) / RATIO >= headContentHeight) {  
  198.                             state = IPullDownElastic.RELEASE_To_REFRESH;  
  199.                             changeHeaderViewByState(state, false);  
  200.                             Logger.i(TAG, "由done或者下拉刷新状态转变到松开刷新");  
  201.                         } else if (tempY - startY <= 0) {  
  202.                             state = IPullDownElastic.DONE;  
  203.                             changeHeaderViewByState(state, false);  
  204.                             Logger.i(TAG, "由DOne或者下拉刷新状态转变到done状态");  
  205.                         }  
  206.                     }  
  207.                     if (tempY - startY > 0) {  
  208.                         setMargin((tempY - startY)/2 + refreshTargetTop);  
  209.                     }  
  210.                 }  
  211.                 break;  
  212.             }  
  213.         }  
  214.     }  
  215.       
  216.     /** 
  217.      *  
  218.      */  
  219.     private void setMargin(int top) {  
  220.         LinearLayout.LayoutParams lp = (LayoutParams) mElastic.getElasticLayout()  
  221.                 .getLayoutParams();  
  222.         lp.topMargin = top;  
  223.         // 修改后刷新  
  224.         mElastic.getElasticLayout().setLayoutParams(lp);  
  225.         mElastic.getElasticLayout().invalidate();  
  226.     }  
  227.   
  228.     private void changeHeaderViewByState(int state, boolean isBack) {  
  229.   
  230.         mElastic.changeElasticState(state, isBack);  
  231.         switch (state) {  
  232.         case IPullDownElastic.RELEASE_To_REFRESH:  
  233.             mElastic.showArrow(View.VISIBLE);  
  234.             mElastic.showProgressBar(View.GONE);  
  235.             mElastic.showLastUpdate(View.VISIBLE);  
  236.             mElastic.setTips(note_release_to_refresh);  
  237.   
  238.             mElastic.clearAnimation();  
  239.             mElastic.startAnimation(animation);  
  240.             Logger.i(TAG, "当前状态,松开刷新");  
  241.             break;  
  242.         case IPullDownElastic.PULL_To_REFRESH:  
  243.             mElastic.showArrow(View.VISIBLE);  
  244.             mElastic.showProgressBar(View.GONE);  
  245.             mElastic.showLastUpdate(View.VISIBLE);  
  246.             mElastic.setTips(note_pull_to_refresh);  
  247.   
  248.             mElastic.clearAnimation();  
  249.   
  250.             // 是由RELEASE_To_REFRESH状态转变来的  
  251.             if (isBack) {  
  252.                 mElastic.startAnimation(reverseAnimation);  
  253.             }  
  254.             Logger.i(TAG, "当前状态,下拉刷新");  
  255.             break;  
  256.         case IPullDownElastic.REFRESHING:  
  257.             mElastic.showArrow(View.GONE);  
  258.             mElastic.showProgressBar(View.VISIBLE);  
  259.             mElastic.showLastUpdate(View.GONE);  
  260.             mElastic.setTips(note_refreshing);  
  261.   
  262.             mElastic.clearAnimation();  
  263.             Logger.i(TAG, "当前状态,正在刷新...");  
  264.             break;  
  265.         case IPullDownElastic.DONE:  
  266.             mElastic.showProgressBar(View.GONE);  
  267.             mElastic.clearAnimation();  
  268. //            arrowImageView.setImageResource(R.drawable.goicon);  
  269.             // tipsTextview.setText("下拉刷新");  
  270.             // lastUpdatedTextView.setVisibility(View.VISIBLE);  
  271.             Logger.i(TAG, "当前状态,done");  
  272.             break;  
  273.         }  
  274.     }  
  275.   
  276.     private void onRefresh() {  
  277.         // downTextView.setVisibility(View.GONE);  
  278. //        scroller.startScroll(0, i, 0, 0 - i);  
  279. //        invalidate();  
  280.         if (refreshListener != null) {  
  281.             refreshListener.onRefresh(this);  
  282.         }  
  283.     }  
  284.   
  285.     /** 
  286.      *  
  287.      */  
  288.     @Override  
  289.     public void computeScroll() {  
  290. //        if (scroller.computeScrollOffset()) {  
  291. //            int i = this.scroller.getCurrY();  
  292. //            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.refreshView  
  293. //                    .getLayoutParams();  
  294. //            int k = Math.max(i, refreshTargetTop);  
  295. //            lp.topMargin = k;  
  296. //            this.refreshView.setLayoutParams(lp);  
  297. //            this.refreshView.invalidate();  
  298. //            invalidate();  
  299. //        }  
  300.     }  
  301.   
  302.     /** 
  303.      * 结束刷新事件,UI刷新完成后必须回调此方法 
  304.      * @param text 一般传入:“上次更新时间:12:23” 
  305.      */  
  306.     public void finishRefresh(String text) {  
  307.         if (mElastic == null) {  
  308.             Logger.d(TAG, "finishRefresh mElastic:" + mElastic);  
  309.             return;  
  310.         }  
  311.         state = IPullDownElastic.DONE;  
  312.         mElastic.setLastUpdateText(text);  
  313.         changeHeaderViewByState(state,false);  
  314.         Logger.i(TAG, "执行了=====finishRefresh");  
  315.   
  316.         mElastic.showArrow(View.VISIBLE);  
  317.         mElastic.showLastUpdate(View.VISIBLE);  
  318.         setMargin(refreshTargetTop);  
  319. //        scroller.startScroll(0, i, 0, refreshTargetTop);  
  320. //        invalidate();  
  321.     }  
  322.   
  323.     private boolean canScroll() {  
  324.         View childView;  
  325.         if (getChildCount() > 1) {  
  326.             childView = this.getChildAt(1);  
  327.             if (childView instanceof AbsListView) {  
  328.                 int top = ((AbsListView) childView).getChildAt(0).getTop();  
  329.                 int pad = ((AbsListView) childView).getListPaddingTop();  
  330.                 if ((Math.abs(top - pad)) < 3  
  331.                         && ((AbsListView) childView).getFirstVisiblePosition() == 0) {  
  332.                     return true;  
  333.                 } else {  
  334.                     return false;  
  335.                 }  
  336.             } else if (childView instanceof ScrollView) {  
  337.                 if (((ScrollView) childView).getScrollY() == 0) {  
  338.                     return true;  
  339.                 } else {  
  340.                     return false;  
  341.                 }  
  342.             }  
  343.   
  344.         }  
  345.         return canScroll(this);  
  346.     }  
  347.       
  348.     /** 
  349.      * 子类重写此方法可以兼容其它的子控件,目前只兼容AbsListView和ScrollView 
  350.      * @param view 
  351.      * @return 
  352.      */  
  353.     public boolean canScroll(PullDownScrollView view) {  
  354.         return false;  
  355.     }  
  356.   
  357.     private void printMotionEvent(MotionEvent event) {  
  358.         switch (event.getAction()) {  
  359.         case MotionEvent.ACTION_DOWN:  
  360.             Logger.d(TAG, "down");  
  361.             break;  
  362.         case MotionEvent.ACTION_MOVE:  
  363.             Logger.d(TAG, "move");  
  364.             break;  
  365.         case MotionEvent.ACTION_UP:  
  366.             Logger.d(TAG, "up");  
  367.         default:  
  368.             break;  
  369.         }  
  370.     }  
  371.     /** 
  372.      * 刷新监听接口 
  373.      */  
  374.     public interface RefreshListener {  
  375.         public void onRefresh(PullDownScrollView view);  
  376.     }  
  377.   
  378. }  

 

4.接口:

Java代码  收藏代码
  1. import android.view.View;  
  2. import android.view.animation.Animation;  
  3.   
  4. /** 
  5.  * @author xwangly@163.com 
  6.  * @date 2013-7-10 
  7.  * 下拉控件接口 
  8.  */  
  9. public interface IPullDownElastic {  
  10.     public final static int RELEASE_To_REFRESH = 0;  
  11.     public final static int PULL_To_REFRESH = 1;  
  12.     public final static int REFRESHING = 2;  
  13.     public final static int DONE = 3;  
  14.   
  15.     public View getElasticLayout();  
  16.   
  17.     public int getElasticHeight();  
  18.   
  19.     public void showArrow(int visibility);  
  20.   
  21.     public void startAnimation(Animation animation);  
  22.   
  23.     public void clearAnimation();  
  24.   
  25.     public void showProgressBar(int visibility);  
  26.   
  27.     public void setTips(String tips);  
  28.   
  29.     public void showLastUpdate(int visibility);  
  30.   
  31.     public void setLastUpdateText(String text);  
  32.       
  33.     /** 
  34.      * 可以不用实现此方法,PullDownScrollView会处理ElasticLayout布局中的状态  
  35.      * 如果需要特殊处理,可以实现此方法进行处理 
  36.      *  
  37.      * @param state  @see RELEASE_To_REFRESH 
  38.      * @param isBack 是否是松开回退 
  39.      */  
  40.     public void changeElasticState(int state, boolean isBack);  
  41.   
  42. }  

 5.默认实现:

Java代码  收藏代码
  1. import android.content.Context;  
  2. import android.view.LayoutInflater;  
  3. import android.view.View;  
  4. import android.view.animation.Animation;  
  5. import android.widget.ImageView;  
  6. import android.widget.ProgressBar;  
  7. import android.widget.TextView;  
  8.   
  9.   
  10. /** 
  11.  * @author xwangly@163.com 
  12.  * @date   2013-7-10 
  13.  * 默认下拉控件布局实现 
  14.  */  
  15. public class PullDownElasticImp implements IPullDownElastic {  
  16.     private View refreshView;  
  17.     private ImageView arrowImageView;  
  18.     private int headContentHeight;  
  19.     private ProgressBar progressBar;  
  20.     private TextView tipsTextview;  
  21.     private TextView lastUpdatedTextView;  
  22.       
  23.     private Context mContext;  
  24.     public PullDownElasticImp(Context context) {  
  25.         mContext = context;  
  26.         init();  
  27.     }  
  28.       
  29.   
  30.     private void init() {  
  31.         // 刷新视图顶端的的view  
  32.         refreshView = LayoutInflater.from(mContext).inflate(  
  33.                 R.layout.refresh_top_item, null);  
  34.   
  35.         // 指示器view  
  36.         arrowImageView = (ImageView) refreshView  
  37.                 .findViewById(R.id.head_arrowImageView);  
  38.         // 刷新bar  
  39.         progressBar = (ProgressBar) refreshView  
  40.                 .findViewById(R.id.head_progressBar);  
  41.         // 下拉显示text  
  42.         tipsTextview = (TextView) refreshView.findViewById(R.id.refresh_hint);  
  43.         // 下来显示时间  
  44.         lastUpdatedTextView = (TextView) refreshView  
  45.                 .findViewById(R.id.refresh_time);  
  46.   
  47.         headContentHeight = Utils.dip2px(mContext, 50);  
  48.     }  
  49.   
  50.     /** 
  51.      * @return 
  52.      *  
  53.      */  
  54.     @Override  
  55.     public View getElasticLayout() {  
  56.         return refreshView;  
  57.     }  
  58.   
  59.     /** 
  60.      * @return 
  61.      *  
  62.      */  
  63.     @Override  
  64.     public int getElasticHeight() {  
  65.         return headContentHeight;  
  66.     }  
  67.   
  68.     /** 
  69.      * @param show 
  70.      *  
  71.      */  
  72.     @Override  
  73.     public void showArrow(int visibility) {  
  74.         arrowImageView.setVisibility(visibility);  
  75.     }  
  76.   
  77.     /** 
  78.      * @param animation 
  79.      *  
  80.      */  
  81.     @Override  
  82.     public void startAnimation(Animation animation) {  
  83.         arrowImageView.startAnimation(animation);  
  84.     }  
  85.   
  86.     /** 
  87.      *  
  88.      *  
  89.      */  
  90.     @Override  
  91.     public void clearAnimation() {  
  92.         arrowImageView.clearAnimation();  
  93.     }  
  94.   
  95.     /** 
  96.      * @param show 
  97.      *  
  98.      */  
  99.     @Override  
  100.     public void showProgressBar(int visibility) {  
  101.         progressBar.setVisibility(visibility);  
  102.     }  
  103.   
  104.     /** 
  105.      * @param tips 
  106.      *  
  107.      */  
  108.     @Override  
  109.     public void setTips(String tips) {  
  110.         tipsTextview.setText(tips);  
  111.     }  
  112.   
  113.     /** 
  114.      * @param show 
  115.      *  
  116.      */  
  117.     @Override  
  118.     public void showLastUpdate(int visibility) {  
  119.         lastUpdatedTextView.setVisibility(visibility);  
  120.     }  
  121.   
  122.     /** 
  123.      * @param text 
  124.      *  
  125.      */  
  126.     public void setLastUpdateText(String text) {  
  127.         lastUpdatedTextView.setText(text);  
  128.     }  
  129.   
  130.   
  131.     /** 
  132.      * @param state 
  133.      * @param isBack 
  134.      *  
  135.      */  
  136.     @Override  
  137.     public void changeElasticState(int state, boolean isBack) {  
  138.         // TODO Auto-generated method stub  
  139.           
  140.     }  
  141.   
  142. }  

 

6.默认实现的布局:

Java代码  收藏代码
  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="-50.0dip"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="0.0dip"  
  10.         android:layout_weight="1.0"  
  11.         android:gravity="center"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <!-- 箭头图像、进度条 -->  
  15.   
  16.         <FrameLayout  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:layout_alignParentLeft="true"  
  20.             android:layout_centerVertical="true"  
  21.             android:layout_marginLeft="30dip" >  
  22.   
  23.             <!-- 箭头 -->  
  24.   
  25.             <ImageView  
  26.                 android:id="@+id/head_arrowImageView"  
  27.                 android:layout_width="wrap_content"  
  28.                 android:layout_height="wrap_content"  
  29.                 android:layout_gravity="center"  
  30.                 android:src="@drawable/goicon" />  
  31.   
  32.             <!-- 进度条 -->  
  33.   
  34.             <ProgressBar  
  35.                 android:id="@+id/head_progressBar"  
  36.                 style="@android:style/Widget.ProgressBar.Small.Inverse"  
  37.                 android:layout_width="wrap_content"  
  38.                 android:layout_height="wrap_content"  
  39.                 android:layout_gravity="center"  
  40.                 android:visibility="gone" />  
  41.         </FrameLayout>  
  42.   
  43.         <LinearLayout  
  44.             android:layout_width="fill_parent"  
  45.             android:layout_height="fill_parent"  
  46.             android:gravity="center"  
  47.             android:orientation="vertical" >  
  48.   
  49.             <!-- 提示 -->  
  50.   
  51.             <TextView  
  52.                 android:id="@+id/refresh_hint"  
  53.                 android:layout_width="wrap_content"  
  54.                 android:layout_height="wrap_content"  
  55.                 android:text="下拉刷新"  
  56.                 android:textColor="#f2f2f2"  
  57.                 android:textSize="16sp" />  
  58.   
  59.             <!-- 最近更新 -->  
  60.   
  61.             <TextView  
  62.                 android:id="@+id/refresh_time"  
  63.                 android:layout_width="wrap_content"  
  64.                 android:layout_height="wrap_content"  
  65.                 android:text="上次更新"  
  66.                 android:textColor="#b89766"  
  67.                 android:textSize="10sp" />  
  68.         </LinearLayout>  
  69.     </LinearLayout>  
  70.   
  71. </LinearLayout>  

 6.图片资源:

@drawable/goicon



 

完结