Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 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.