转载自http://blog.sina.com.cn/s/blog_7575ed8b01013cnp.html
我已经试过了,效果蛮好,这是转载篇,下一篇我将我的贡献出来,在此感谢楼上那位博主,他的源码如下:
今天项目中遇到一个问题:
下拉滑动工单列表时(GridView/ListView实现)刷新最新数据,类似新浪微博中顶端下拉刷新数据功能。
在网上搜了一些解决方案,很多方法看起来很复杂,看了几个方案,选择了一个简单的,自己也修改了一下,实现起来比较好的与大家一起分享,相信大家以后会用的到(本篇先分享ListView的实现方式,下一篇在实现GridView的下拉刷新数据功能),乍一看比较长,大家细看一下,就知道比较简单了。
先把图依次展示一下吧(图中刷新了2次了所以时间会出现2个)
//复写了ListView的带下拉刷新功能的PullToRefreshListView
packagesrc.com.lyg.refreshlist;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importsrc.com.lyg.R;
importandroid.content.Context;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.animation.LinearInterpolator;
importandroid.view.animation.RotateAnimation;
importandroid.widget.AbsListView;
importandroid.widget.AbsListView.OnScrollListener;
importandroid.widget.BaseAdapter;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.ListView;
importandroid.widget.ProgressBar;
importandroid.widget.TextView;
public classPullToRefreshListView extends ListView implements OnScrollListener{
private staticfinal String TAG = "listview";
private finalstatic int RELEASE_To_REFRESH = 0; // 释放
private finalstatic int PULL_To_REFRESH = 1;// 下拉刷新
private finalstatic int REFRESHING = 2; // 正在刷新
private finalstatic int DONE = 3; // 按下
private finalstatic int LOADING = 4;
//实际的padding的距离与界面上偏移距离的比例
private finalstatic int RATIO = 3;
privateLayoutInflater inflater;
privateLinearLayout headView;
privateTextView tipsTextview;
privateTextView lastUpdatedTextView;
privateImageView arrowImageView;
privateProgressBar progressBar;
privateRotateAnimation animation;
privateRotateAnimation reverseAnimation;
//用于保证startY的值在一个完整的touch事件中只被记录一次
private booleanisRecored;
private intheadContentWidth;
private intheadContentHeight;
private intstartY;
private intfirstItemIndex;
private intstate;
private booleanisBack;
privateOnRefreshListener refreshListener;
private booleanisRefreshable;
init(context);
//setCacheColorHint(context.getResources().getColor(R.color.transparent));
inflater =LayoutInflater.from(context);
// head布局文件
headView =(LinearLayout) inflater.inflate(R.layout.head, null);
//下拉箭头
arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);
arrowImageView.setMinimumWidth(70);
arrowImageView.setMinimumHeight(50);
//进度条
progressBar = (ProgressBar) headView.findViewById(R.id.head_progressBar);
// 下拉提示刷新
tipsTextview =(TextView)headView.findViewById(R.id.head_tipsTextView);
//最新一次刷新时间
lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);
measureView(headView);
headContentHeight =headView.getMeasuredHeight();
headContentWidth =headView.getMeasuredWidth();
//初始状态是 隐藏掉head布局
headView.setPadding(0, -1 * headContentHeight,0, 0);
headView.invalidate();
public booleanonTouchEvent(MotionEvent event) {
if(isRefreshable) {
switch(event.getAction()) {
//在down时候记录当前Y的位置
caseMotionEvent.ACTION_DOWN:
if(firstItemIndex == 0 && !isRecored){
isRecored =true;
startY = (int)event.getY();
Log.v(TAG,"在down时候记录当前位置‘");
}
break;
caseMotionEvent.ACTION_UP:
if (state !=REFRESHING && state != LOADING){
if (state ==DONE) {
//什么都不做
}
//由下拉刷新状态,到done状态
if (state ==PULL_To_REFRESH) {
state =DONE;
changeHeaderViewByState();
Log.v(TAG,"由下拉刷新状态,到done状态");
}
if (state ==RELEASE_To_REFRESH) {
state =REFRESHING;
changeHeaderViewByState();
onRefresh();
Log.v(TAG,"由松开刷新状态,到done状态");
}
}
isRecored =false;
isBack =false;
break;
caseMotionEvent.ACTION_MOVE:
int tempY =(int) event.getY();
if (!isRecored&& firstItemIndex == 0){
Log.v(TAG,"在move时候记录下位置");
isRecored =true;
startY =tempY;
}
if (state !=REFRESHING && isRecored&& state != LOADING) {
//保证在设置padding的过程中,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动
//可以松手去刷新了
if (state ==RELEASE_To_REFRESH) {
setSelection(0);
//往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步
if(((tempY - startY) / RATIO <headContentHeight)&&(tempY - startY) > 0) {
state =PULL_To_REFRESH;
changeHeaderViewByState();
Log.v(TAG,"由松开刷新状态转变到下拉刷新状态");
}
//一下子推到顶了
else if (tempY- startY <= 0) {
state =DONE;
changeHeaderViewByState();
Log.v(TAG,"由松开刷新状态转变到done状态");
}
//往下拉了,或者还没有上推到屏幕顶部掩盖head的地步
else{
//不用进行特别的操作,只用更新paddingTop的值就行了
}
}
//还没有到达显示松开刷新的时候,DONE或者是PULL_To_REFRESH状态
if (state ==PULL_To_REFRESH) {
setSelection(0);
//下拉到可以进入RELEASE_TO_REFRESH的状态
if ((tempY -startY) / RATIO >= headContentHeight) {
state =RELEASE_To_REFRESH;
isBack =true;
changeHeaderViewByState();
Log.v(TAG,"由done或者下拉刷新状态转变到松开刷新");
}
//上推到顶了
else if (tempY- startY <= 0) {
state =DONE;
changeHeaderViewByState();
Log.v(TAG,"由DOne或者下拉刷新状态转变到done状态");
}
}
//done状态下
if (state ==DONE) {
if (tempY -startY > 0) {
state =PULL_To_REFRESH;
changeHeaderViewByState();
}
}
//更新headView的size
if (state ==PULL_To_REFRESH) {
headView.setPadding(0, -1 *headContentHeight
+ (tempY -startY) / RATIO, 0, 0);
}
//更新headView的paddingTop
if (state ==RELEASE_To_REFRESH) {
headView.setPadding(0, (tempY - startY) /RATIO- headContentHeight, 0,0);
}
}
break;
}
}
returnsuper.onTouchEvent(event);
}
}
//head.xml布局文件
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
<!-- 内容-->
</LinearLayout>
//activity页面展示
packagesrc.com.lyg.refreshlist;
importjava.util.LinkedList;
importsrc.com.lyg.R;
importsrc.com.lyg.refreshlist.PullToRefreshListView.OnRefreshListener;
importandroid.app.Activity;
importandroid.os.AsyncTask;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.TextView;
public classPullToRefreshListViewAct ivity extends Activity {
privateLinkedList<String> data;
private BaseAdapteradapter;
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 模拟数据
data = newLinkedList<String>();
for (int i = 0; i< 10; i++) {
data.add(String.valueOf(i));
}
//带下拉刷新功能复写了ListView的PullToRefreshListView
final PullToRefreshListViewlistView =(PullToRefreshListView)findViewById(R.id.order_form_list);
// 适配器
adapter = new BaseAdapter(){
public long getItemId(intposition) {
return position;
}
public Object getItem(intposition) {
returndata.get(position);
}
public int getCount(){
returndata.size();
}
public View getView(intposition, View convertView, ViewGroup parent) {
convertView =LayoutInflater.from(getApplicationContext()).inflate(R.layout.item,null);
TextView textView = (TextView)convertView.findViewById(R.id.textView_item);
textView.setText(data.get(position));
returnconvertView;
}
};
listView.setAdapter(adapter);
// 下拉刷新监听
listView.setonRefreshListener(newOnRefreshListener() {
public void onRefresh(){
newAsyncTask<Void, Void, Void>(){
protected VoiddoInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (Exception e){
e.printStackTrace();
}
data.addFirst("刷新后的内容");
return null;
}
@Override
protected voidonPostExecute(Void result) {
adapter.notifyDataSetChanged();
listView.onRefreshComplete();
}
}.execute(null);
}
});
}
}
//activity里面的main.xml布局文件
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>