Android模仿QQ空间的下拉更新的功能实现


看看今天模仿的QQ空间的下拉更新个应用吧

首先看看布局

1.main.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.   
  7.     <EditText  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:drawableLeft="@drawable/write"  
  11.         android:lines="1"  
  12.         android:padding="5px"  
  13.         android:singleLine="true"  
  14.         android:text="写说说" />  
  15.   
  16.     <TabHost  
  17.         android:id="@+id/tabhost"  
  18.         android:background="@drawable/bg"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="match_parent" >  
  21.   
  22.         <LinearLayout  
  23.             android:id="@+id/linearLayout1"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="match_parent"  
  26.             android:orientation="vertical" >  
  27.   
  28.             <TabWidget  
  29.                 android:id="@android:id/tabs"  
  30.                 android:layout_width="match_parent"  
  31.                 android:layout_height="wrap_content" >  
  32.             </TabWidget>  
  33.   
  34.             <FrameLayout  
  35.                 android:id="@android:id/tabcontent"  
  36.                 android:layout_width="match_parent"  
  37.                 android:layout_height="match_parent" >  
  38.   
  39.                 <LinearLayout  
  40.                     android:id="@+id/tab1"  
  41.                     android:layout_width="match_parent"  
  42.                     android:layout_height="match_parent"  
  43.                     >  
  44.   
  45.                     <com.wang.MyListView  
  46.                         android:id="@+id/listView"  
  47.                         android:layout_width="fill_parent"  
  48.                         android:layout_height="fill_parent"  
  49.                         />  
  50.                 </LinearLayout>  
  51.   
  52.                 <LinearLayout  
  53.                     android:id="@+id/tab2"  
  54.                     android:layout_width="match_parent"  
  55.                     android:layout_height="match_parent"  
  56.                      >  
  57.                 </LinearLayout>  
  58.   
  59.                 <LinearLayout  
  60.                     android:id="@+id/tab3"  
  61.                     android:layout_width="match_parent"  
  62.                     android:layout_height="match_parent"  
  63.                      >  
  64.                 </LinearLayout>  
  65.             </FrameLayout>  
  66.         </LinearLayout>  
  67.     </TabHost>  
  68.   
  69. </LinearLayout>  
2.list的条目布局list_item.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:gravity="center_vertical"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/imageView_item"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginLeft="5dp"  
  13.         android:src="@drawable/icon" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/textView_item"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_marginLeft="10dp"  
  20.         android:textColor="#FFFFFF"  
  21.         />  
  22.   
  23. </LinearLayout>  
3. 刷新布局refresh.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="wrap_content" >  
  5.   
  6.     <RelativeLayout  
  7.         android:id="@+id/head_contentLayout"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:paddingLeft="30dp" >  
  11.   
  12.         <FrameLayout  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_alignParentLeft="true"  
  16.             android:layout_centerVertical="true" >  
  17.   
  18.             <ImageView  
  19.                 android:id="@+id/head_arrowImageView"  
  20.                 android:layout_width="wrap_content"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:layout_gravity="center"  
  23.                 android:src="@drawable/down" />  
  24.   
  25.             <ProgressBar  
  26.                 android:id="@+id/head_progressBar"  
  27.                 style="?android:attr/progressBarStyleSmall"  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:layout_gravity="center"  
  31.                 android:visibility="gone" />  
  32.         </FrameLayout>  
  33.   
  34.         <LinearLayout  
  35.             android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_centerHorizontal="true"  
  38.             android:gravity="center_horizontal"  
  39.             android:orientation="vertical" >  
  40.   
  41.             <TextView  
  42.                 android:id="@+id/head_tipsTextView"  
  43.                 android:layout_width="wrap_content"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:text="下拉可以刷新"  
  46.                 android:textSize="15dp" />  
  47.   
  48.             <TextView  
  49.                 android:id="@+id/head_lastUpdatedTextView"  
  50.                 android:layout_width="wrap_content"  
  51.                 android:layout_height="wrap_content"  
  52.                 android:text="上次更新时间:"  
  53.                 android:textSize="12dp" />  
  54.         </LinearLayout>  
  55.     </RelativeLayout>  
  56.   
  57. </LinearLayout>  
4.看看主活动实现的过程PullrefreshDemoActivity.xml
  1. package com.wang;  
  2.   
  3. import java.util.LinkedList;  
  4.   
  5. import com.wang.MyListView.OnRefreshListener;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.res.Resources;  
  9. import android.os.AsyncTask;  
  10. import android.os.Bundle;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.view.Window;  
  15. import android.view.WindowManager;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.TabHost;  
  18. import android.widget.TextView;  
  19.   
  20. public class PullrefreshDemoActivity extends Activity {  
  21.     private LinkedList<String> data;  
  22.     private BaseAdapter adapter;  
  23.   
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         // 去除标题栏   
  26.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  27.         // 设置全屏,取消状态栏   
  28.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  29.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.           
  33.          TabHost tabhost=(TabHost)findViewById(R.id.tabhost);  
  34.             tabhost.setup();  
  35.             tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("好友动态").setContent(R.id.tab1));  
  36.               
  37.             tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("我的动态").setContent(R.id.tab2));  
  38.             tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("我的应用").setContent(R.id.tab3));  
  39.   
  40.         data = new LinkedList<String>();  
  41.         for (int i = 30; i < 40; i++) {  
  42.             // 输出list——item上的数据   
  43.             data.add(String.valueOf("暑假第" + i + "天,我们一直很宅,想家ing ..."));  
  44.         }  
  45.   
  46.         final MyListView listView = (MyListView) findViewById(R.id.listView);  
  47.         adapter = new BaseAdapter() {  
  48.   
  49.             // 得到一个视图,显示在指定位置上的数据在数据集,可以创建一个视图从XML布局文件   
  50.             public View getView(int position, View convertView, ViewGroup parent) {  
  51.                 // 上下文全局应用程序对象   
  52.                 convertView = LayoutInflater.from(getApplicationContext())  
  53.                         .inflate(R.layout.list_item, null);  
  54.                 // 实例化组件   
  55.                 TextView textView = (TextView) convertView  
  56.                         .findViewById(R.id.textView_item);  
  57.                 // 设置文本本内容   
  58.                 textView.setText(data.get(position));  
  59.                 return convertView;  
  60.             }  
  61.   
  62.             // 得到行相关联id列表中指定的位置。   
  63.             public long getItemId(int position) {  
  64.                 return position;  
  65.             }  
  66.   
  67.             // 获得相关的数据项中的指定位置的数据集   
  68.             public Object getItem(int position) {  
  69.                 return data.get(position);  
  70.             }  
  71.   
  72.             // 获得项目在数据集适配器的个数。   
  73.             public int getCount() {  
  74.                 return data.size();  
  75.             }  
  76.   
  77.         };  
  78.   
  79.         listView.setAdapter(adapter);  
  80.   
  81.         listView.setonRefreshListener(new OnRefreshListener() {  
  82.             public void onRefresh() {  
  83.   
  84.                 new AsyncTask<Void, Void, Void>() {  
  85.                     // ...b表示多个参数   
  86.                     protected Void doInBackground(Void... params) {  
  87.                         try {  
  88.                             //    
  89.                             Thread.sleep(1000);  
  90.                         } catch (Exception e) {  
  91.                             e.printStackTrace();  
  92.                         }  
  93.                               
  94.                             // 增加一条数据到list中   
  95.                             data.addFirst("刷新后内容:每天都是新的一天!!!,親!要努力奋斗哦!!!");  
  96.                           
  97.                         return null;  
  98.                     }  
  99.   
  100.                     protected void onPostExecute(Void result) {  
  101.                         adapter.notifyDataSetChanged();  
  102.                         listView.onRefreshComplete();  
  103.                     }  
  104.   
  105.                 }.execute(null);  
  106.             }  
  107.         });  
  108.     }  
  109. }  
5.接着是列表实现个过程MyListView.xml
  1. package com.wang;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. import android.content.Context;  
  7. import android.util.AttributeSet;  
  8. import android.util.Log;  
  9. import android.view.LayoutInflater;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.view.animation.LinearInterpolator;  
  14. import android.view.animation.RotateAnimation;  
  15. import android.widget.AbsListView;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.ImageView;  
  18. import android.widget.LinearLayout;  
  19. import android.widget.ListView;  
  20. import android.widget.AbsListView.OnScrollListener;  
  21. import android.widget.ProgressBar;  
  22. import android.widget.TextView;  
  23.   
  24. public class MyListView extends ListView implements OnScrollListener {  
  25.   
  26.     private static final String TAG = "listview";  
  27.   
  28.     private final static int RELEASE_To_REFRESH = 0;  
  29.     private final static int PULL_To_REFRESH = 1;  
  30.     private final static int REFRESHING = 2;  
  31.     private final static int DONE = 3;  
  32.     private final static int LOADING = 4;  
  33.   
  34.     // 实际的padding的距离与界面上偏移距离的比例   
  35.     private final static int RATIO = 3;  
  36.   
  37.     private LayoutInflater inflater;  
  38.   
  39.     private LinearLayout headView;  
  40.   
  41.     private TextView tipsTextview;  
  42.     private TextView lastUpdatedTextView;  
  43.     private ImageView arrowImageView;  
  44.     private ProgressBar progressBar;  
  45.   
  46.     private RotateAnimation animation;  
  47.     private RotateAnimation reverseAnimation;  
  48.   
  49.     // 用于保证startY的值在一个完整的touch事件中只被记录一次   
  50.     private boolean isRecored;  
  51.   
  52.     private int headContentWidth;  
  53.     private int headContentHeight;  
  54.   
  55.     private int startY;  
  56.     private int firstItemIndex;  
  57.   
  58.     private int state;  
  59.   
  60.     private boolean isBack;  
  61.   
  62.     private OnRefreshListener refreshListener;  
  63.   
  64.     private boolean isRefreshable;  
  65.   
  66.     public MyListView(Context context) {  
  67.         super(context);  
  68.         // 调用下面初始化的函数   
  69.         init(context);  
  70.     }  
  71.   
  72.     public MyListView(Context context, AttributeSet attrs) {  
  73.         super(context, attrs);  
  74.         // 调用下面初始化的函数   
  75.         init(context);  
  76.     }  
  77.   
  78.     private void init(Context context) {  
  79.   
  80.         // 获得LayoutInflater从给定的上下文。   
  81.         inflater = LayoutInflater.from(context);  
  82.   
  83.         // 实例化布局XML文件转换成相应的视图对象。   
  84.         headView = (LinearLayout) inflater.inflate(R.layout.refresh, null);  
  85.   
  86.         arrowImageView = (ImageView) headView  
  87.                 .findViewById(R.id.head_arrowImageView);  
  88.         // 设置最小宽度 和高度   
  89.         arrowImageView.setMinimumWidth(70);  
  90.         arrowImageView.setMinimumHeight(50);  
  91.         // 实例化布局XML文件转换成相应的视图对象。   
  92.         progressBar = (ProgressBar) headView  
  93.                 .findViewById(R.id.head_progressBar);  
  94.         tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);  
  95.         lastUpdatedTextView = (TextView) headView  
  96.                 .findViewById(R.id.head_lastUpdatedTextView);  
  97.   
  98.         // 调用下拉刷新的方法   
  99.         measureView(headView);  
  100.         // d得到原始高度和宽度   
  101.         headContentHeight = headView.getMeasuredHeight();  
  102.         headContentWidth = headView.getMeasuredWidth();  
  103.   
  104.         // 设置填充。视图可能添加的空间要求显示滚动条,这取决于风格和知名度的滚动条   
  105.         headView.setPadding(0, -1 * headContentHeight, 00);  
  106.         headView.invalidate();  
  107.   
  108.         // 标签用来识别一个日志消息的来源。标识类或活动日志调用发生   
  109.         Log.v("size""width:" + headContentWidth + " height:"  
  110.                 + headContentHeight);  
  111.   
  112.         // 添加一个固定视图出现在列表的顶部   
  113.         addHeaderView(headView, nullfalse);  
  114.         // 设置监听事件   
  115.         setOnScrollListener(this);  
  116.   
  117.         // 动画效果实现下拉和松开时候图片的 180度 旋转 注意0, -180,和他是有区别的 -180, 0,   
  118.         animation = new RotateAnimation(0, -180,  
  119.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  120.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  121.         // 设置加速度曲线为这个动画。默认值为一个线性插值。   
  122.         animation.setInterpolator(new LinearInterpolator());  
  123.         animation.setDuration(300);  
  124.         // 如果fillAfter是真的,转换,该动画执行完成时将会持续下去   
  125.         animation.setFillAfter(true);  
  126.   
  127.         reverseAnimation = new RotateAnimation(-1800,  
  128.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  129.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  130.         // 设置加速度曲线为这个动画。默认值为一个线性插值。   
  131.         reverseAnimation.setInterpolator(new LinearInterpolator());  
  132.         reverseAnimation.setDuration(300);  
  133.         // 如果fillAfter是真的,转换,该动画执行完成时将会持续下去   
  134.         reverseAnimation.setFillAfter(true);  
  135.   
  136.         // 设置状态   
  137.         state = DONE;  
  138.         // 设置不可刷新状态   
  139.         isRefreshable = false;  
  140.     }  
  141.   
  142.     // 回调方法时要调用的列表或网格已经滚动。这将完成之后调用的滚动方法   
  143.     public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,  
  144.             int arg3) {  
  145.         firstItemIndex = firstVisiableItem;  
  146.     }  
  147.   
  148.     /* 
  149.      * 回调方法调用而列表视图或网格视图被滚动。 如果这个视图被滚动,将调用此方法在接下来的一局画卷的呈现 * 
  150.      */  
  151.     public void onScrollStateChanged(AbsListView arg0, int arg1) {  
  152.     }  
  153.   
  154.     // 触摸事件监听   
  155.     public boolean onTouchEvent(MotionEvent event) {  
  156.   
  157.         // 判断是否可以刷新   
  158.         if (isRefreshable) {  
  159.             // 根据动作相应不同的方法   
  160.             switch (event.getAction()) {  
  161.   
  162.             // 当按住屏幕向下拉屏幕的时候   
  163.             case MotionEvent.ACTION_DOWN:  
  164.                 if (firstItemIndex == 0 && !isRecored) {  
  165.                     isRecored = true;  
  166.                     startY = (int) event.getY();  
  167.                     Log.v(TAG, "在下拉的时候记录当前位置‘");  
  168.                 }  
  169.                 break;  
  170.   
  171.             // 当按住屏幕向上松屏幕的时候   
  172.             case MotionEvent.ACTION_UP:  
  173.   
  174.                 if (state != REFRESHING && state != LOADING) {  
  175.                     if (state == DONE) {  
  176.   
  177.                     }  
  178.                     if (state == PULL_To_REFRESH) {  
  179.                         state = DONE;  
  180.                         changeHeaderViewByState();  
  181.   
  182.                         Log.v(TAG, "由下拉刷新状态,到done状态");  
  183.                     }  
  184.                     if (state == RELEASE_To_REFRESH) {  
  185.                         state = REFRESHING;  
  186.                         changeHeaderViewByState();  
  187.                         onRefresh();  
  188.   
  189.                         Log.v(TAG, "由松开刷新状态,到done状态");  
  190.                     }  
  191.                 }  
  192.   
  193.                 isRecored = false;  
  194.                 isBack = false;  
  195.   
  196.                 break;  
  197.   
  198.             // 当按住屏幕移动时候   
  199.             case MotionEvent.ACTION_MOVE:  
  200.                 int tempY = (int) event.getY();  
  201.   
  202.                 if (!isRecored && firstItemIndex == 0) {  
  203.                     Log.v(TAG, "在move时候记录下位置");  
  204.                     isRecored = true;  
  205.                     startY = tempY;  
  206.                 }  
  207.   
  208.                 if (state != REFRESHING && isRecored && state != LOADING) {  
  209.   
  210.                     /*** 
  211.                      * , 当前的位置一直是在head,否则如果当列表超出屏幕的话, 当在上推的时候,列表会同时进行滚动 
  212.                      */  
  213.                     // 可以松手去刷新了   
  214.                     if (state == RELEASE_To_REFRESH) {  
  215.   
  216.                         setSelection(0);  
  217.   
  218.                         // 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步   
  219.                         if (((tempY - startY) / RATIO < headContentHeight)  
  220.                                 && (tempY - startY) > 0) {  
  221.                             state = PULL_To_REFRESH;  
  222.                             changeHeaderViewByState();  
  223.   
  224.                             Log.v(TAG, "由松开刷新状态转变到下拉刷新状态");  
  225.                         }  
  226.                         // 一下子推到顶了   
  227.                         else if (tempY - startY <= 0) {  
  228.                             state = DONE;  
  229.                             // 调用改变时候的方法,更新UI   
  230.                             changeHeaderViewByState();  
  231.   
  232.                             Log.v(TAG, "由松开刷新状态转变到done状态");  
  233.                         } else {  
  234.                         }  
  235.                     }  
  236.                     // 还没有到达显示松开刷新的时候   
  237.                     if (state == PULL_To_REFRESH) {  
  238.   
  239.                         setSelection(0);  
  240.   
  241.                         // 下拉到可以进入RELEASE_TO_REFRESH的状态   
  242.                         if ((tempY - startY) / RATIO >= headContentHeight) {  
  243.                             state = RELEASE_To_REFRESH;  
  244.                             isBack = true;  
  245.                             // 调用改变时候的方法,更新UI   
  246.                             changeHeaderViewByState();  
  247.   
  248.                             Log.v(TAG, "由done或者下拉刷新状态转变到松开刷新");  
  249.                         }  
  250.                         // 上推到顶了   
  251.                         else if (tempY - startY <= 0) {  
  252.                             state = DONE;  
  253.                             // 调用改变时候的方法,更新UI   
  254.                             changeHeaderViewByState();  
  255.   
  256.                             Log.v(TAG, "由DOne或者下拉刷新状态转变到done状态");  
  257.                         }  
  258.                     }  
  259.   
  260.                     // done状态下   
  261.                     if (state == DONE) {  
  262.                         if (tempY - startY > 0) {  
  263.                             state = PULL_To_REFRESH;  
  264.                             // 调用改变时候的方法,更新UI   
  265.                             changeHeaderViewByState();  
  266.                         }  
  267.                     }  
  268.   
  269.                     // 更新headView的size   
  270.                     if (state == PULL_To_REFRESH) {  
  271.                         headView.setPadding(0, -1 * headContentHeight  
  272.                                 + (tempY - startY) / RATIO, 00);  
  273.   
  274.                     }  
  275.   
  276.                     // 更新headView的paddingTop   
  277.                     if (state == RELEASE_To_REFRESH) {  
  278.                         headView.setPadding(0, (tempY - startY) / RATIO  
  279.                                 - headContentHeight, 00);  
  280.                     }  
  281.   
  282.                 }  
  283.   
  284.                 break;  
  285.             }  
  286.         }  
  287.   
  288.         return super.onTouchEvent(event);  
  289.     }  
  290.   
  291.     // 当状态改变时候,调用该方法,以更新界面   
  292.     private void changeHeaderViewByState() {  
  293.         // 根据当前的状态进行判断   
  294.         switch (state) {  
  295.   
  296.         // 下拉时候,松开既可刷新   
  297.         case RELEASE_To_REFRESH:  
  298.             // 设置视图 VISIBLE 可见 ,GONE 不可见   
  299.             arrowImageView.setVisibility(View.VISIBLE);  
  300.             progressBar.setVisibility(View.GONE);  
  301.             tipsTextview.setVisibility(View.VISIBLE);  
  302.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  303.   
  304.             // 现在开始指定的动画。   
  305.             arrowImageView.clearAnimation();  
  306.             arrowImageView.startAnimation(animation);  
  307.   
  308.             tipsTextview.setText("松开既可刷新");  
  309.   
  310.             Log.v(TAG, "当前状态,松开即可刷新");  
  311.             break;  
  312.   
  313.         // 开始时候,下拉刷新   
  314.         case PULL_To_REFRESH:  
  315.             // 设置视图 VISIBLE 可见 ,GONE 不可见   
  316.             progressBar.setVisibility(View.GONE);  
  317.             tipsTextview.setVisibility(View.VISIBLE);  
  318.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  319.             // 现在开始指定的动画。   
  320.             arrowImageView.clearAnimation();  
  321.             arrowImageView.setVisibility(View.VISIBLE);  
  322.   
  323.             if (isBack) {  
  324.                 isBack = false;  
  325.                 // 现在开始指定的动画。   
  326.                 arrowImageView.clearAnimation();  
  327.                 arrowImageView.startAnimation(reverseAnimation);  
  328.   
  329.                 tipsTextview.setText("下拉刷新");  
  330.             } else {  
  331.                 tipsTextview.setText("下拉刷新");  
  332.             }  
  333.             Log.v(TAG, "当前状态,下拉刷新");  
  334.             break;  
  335.   
  336.         case REFRESHING:  
  337.   
  338.             headView.setPadding(0000);  
  339.             // 设置视图 VISIBLE 可见 ,GONE 不可见   
  340.             progressBar.setVisibility(View.VISIBLE);  
  341.             // 现在开始指定的动画。   
  342.             arrowImageView.clearAnimation();  
  343.             arrowImageView.setVisibility(View.GONE);  
  344.             tipsTextview.setText("正在刷新...");  
  345.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  346.   
  347.             Log.v(TAG, "当前状态,正在刷新...");  
  348.             break;  
  349.         case DONE:  
  350.             // 设置填充。视图可能添加的空间要求显示滚动条   
  351.             headView.setPadding(0, -1 * headContentHeight, 00);  
  352.             // 设置视图 VISIBLE 可见 ,GONE 不可见   
  353.             progressBar.setVisibility(View.GONE);  
  354.             // 现在开始指定的动画。   
  355.             arrowImageView.clearAnimation();  
  356.             arrowImageView.setImageResource(R.drawable.down);  
  357.             tipsTextview.setText("下拉刷新");  
  358.             lastUpdatedTextView.setVisibility(View.VISIBLE);  
  359.   
  360.             Log.v(TAG, "当前状态");  
  361.             break;  
  362.         }  
  363.     }  
  364.   
  365.     public void setonRefreshListener(OnRefreshListener refreshListener) {  
  366.         this.refreshListener = refreshListener;  
  367.         isRefreshable = true;  
  368.     }  
  369.   
  370.     public interface OnRefreshListener {  
  371.         public void onRefresh();  
  372.     }  
  373.   
  374.     // 设置更新时间   
  375.     public void onRefreshComplete() {  
  376.         state = DONE;  
  377.         //   
  378.         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");  
  379.         String date = format.format(new Date());  
  380.         lastUpdatedTextView.setText("最近更新:" + date);  
  381.         changeHeaderViewByState();  
  382.     }  
  383.   
  384.     private void onRefresh() {  
  385.         if (refreshListener != null) {  
  386.             refreshListener.onRefresh();  
  387.         }  
  388.     }  
  389.   
  390.     // 下拉刷新的   
  391.     private void measureView(View child) {  
  392.         // v这组布局参数宽度和高度   
  393.         ViewGroup.LayoutParams p = child.getLayoutParams();  
  394.         if (p == null) {  
  395.             // 创建一个新组布局参数指定的宽度(填充)和高度(包裹)。   
  396.             p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,  
  397.                     ViewGroup.LayoutParams.WRAP_CONTENT);  
  398.         }  
  399.   
  400.         // d得到childWidthSpec(高度或宽度)的子视图   
  401.         int childWidthSpec = ViewGroup.getChildMeasureSpec(00 + 0, p.width);  
  402.         int lpHeight = p.height;  
  403.         int childHeightSpec;  
  404.         if (lpHeight > 0) {  
  405.             // 创建一个测量规范基于所提供的大小和模式   
  406.             childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,  
  407.                     MeasureSpec.EXACTLY);  
  408.         } else {  
  409.             childHeightSpec = MeasureSpec.makeMeasureSpec(0,  
  410.                     MeasureSpec.UNSPECIFIED);  
  411.         }  
  412.         // 找出一个视图应该多大。父供应约束信息在宽度和高度参数   
  413.         child.measure(childWidthSpec, childHeightSpec);  
  414.     }  
  415.   
  416.     public void setAdapter(BaseAdapter adapter) {  
  417.         // 设置最近刷新的时间   
  418.         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");  
  419.         String date = format.format(new Date());  
  420.         lastUpdatedTextView.setText("最近更新:" + date);  
  421.         super.setAdapter(adapter);  
  422.     }  
  423.   
  424. }  

6.接着就可以看到运行结果了,运行结果如下:

相关内容