效果图:
PullToRefreshListView.java
package com.example.pulltorefresh;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.Inflater;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class PullToRefreshListView extends ListView {
private int viewHeight;
private int viewWidth;
private int startY;
private int endY;
private int dy;
private int paddingY;
private View view;
private static final int STATE_PULL_TO_REFRESH = 1;
private static final int RELEASR_TO_REFRESH = 2;
private static final int STATE_REFRESHING = 3;
private static int mCurrentState = STATE_PULL_TO_REFRESH;
private ImageView ivArrow;
private TextView tvRefresh;
private TextView tvTime;
private RotateAnimation animUp;
private RotateAnimation animDown;
private ProgressBar pbLoad;
public PullToRefreshListView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
public PullToRefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public PullToRefreshListView(Context context) {
super(context);
initView();
}
private void initView() {
view = View.inflate(getContext(), R.layout.pull_to_refresh, null);
this.addHeaderView(view);
ivArrow = (ImageView) view.findViewById(R.id.iv_arrow);
tvRefresh = (TextView) view.findViewById(R.id.tv_refresh);
tvTime = (TextView) view.findViewById(R.id.tv_time);
pbLoad = (ProgressBar) view.findViewById(R.id.pb_load);
view.measure(0, 0);
viewHeight = view.getMeasuredHeight();
viewWidth = view.getMeasuredWidth();
view.setPadding(0, -viewHeight, 0, 0);
initAnim();
setCurrentTime();
}
private void initAnim() {
animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animUp.setDuration(200);
animUp.setFillAfter(true);
animDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
animDown.setDuration(200);
animDown.setFillAfter(true);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
endY = (int) ev.getY();
dy = endY - startY;
if(mCurrentState == STATE_REFRESHING){
break;
}
int firstVisiblePosition = getFirstVisiblePosition();
if (dy > 0 && firstVisiblePosition == 0) {
paddingY = dy - viewHeight;
view.setPadding(0, paddingY, 0, 0);
if (paddingY < 0 && mCurrentState != STATE_PULL_TO_REFRESH) {
// 下拉刷新
mCurrentState = STATE_PULL_TO_REFRESH;
refreshState();
} else if (paddingY >= 0 && mCurrentState != RELEASR_TO_REFRESH) {
// 松开刷新
mCurrentState = RELEASR_TO_REFRESH;
refreshState();
}
return true;
}
break;
case MotionEvent.ACTION_UP:
if(mCurrentState == RELEASR_TO_REFRESH){
mCurrentState = STATE_REFRESHING;
refreshState();
view.setPadding(0, 0, 0, 0);
//4.进行回调
if(mListenner!=null){
mListenner.onRefresh();
}
}else if (mCurrentState == STATE_PULL_TO_REFRESH){
view.setPadding(0, -viewHeight, 0, 0);
}
break;
default:
break;
}
return super.onTouchEvent(ev);
}
/**
* 根据当前状态刷新界面
*/
private void refreshState() {
// TODO Auto-generated method stub
switch (mCurrentState) {
case STATE_PULL_TO_REFRESH:
tvRefresh.setText("下拉刷新");
ivArrow.startAnimation(animDown);
pbLoad.setVisibility(View.INVISIBLE);
ivArrow.setVisibility(View.VISIBLE);
break;
case RELEASR_TO_REFRESH:
tvRefresh.setText("松开刷新");
ivArrow.startAnimation(animUp);
pbLoad.setVisibility(View.INVISIBLE);
ivArrow.setVisibility(View.VISIBLE);
break;
case STATE_REFRESHING:
tvRefresh.setText("正在刷新");
pbLoad.setVisibility(View.VISIBLE);
ivArrow.clearAnimation();
ivArrow.setVisibility(View.INVISIBLE);
break;
default:
break;
}
}
/**
* 设置刷新时间显示
*/
private void setCurrentTime(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = format.format(new Date());
tvTime.setText(time);
}
/**
* 刷新结束操作
*/
public void onRefreshFinished(){
view.setPadding(0, -viewHeight, 0, 0);
tvRefresh.setText("下拉刷新");
pbLoad.setVisibility(View.INVISIBLE);
ivArrow.setVisibility(View.VISIBLE);
setCurrentTime();
}
//3.定义成员变量
public onRefreshListenner mListenner;
//2.定义一个方法暴露接口
public void setOnRefreshListenner(onRefreshListenner listenner){
mListenner=listenner;
}
//1.定义刷新接口
public interface onRefreshListenner{
public void onRefresh();
}
}
对应的界面pull_to_refresh.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp" >
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/common_listview_headview_red_arrow" />
<ProgressBar
android:id="@+id/pb_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉刷新"
android:textColor="#f00"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2017-3-23 11:30"
android:layout_marginTop="3dp"
android:textColor="#c000"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
主函数MainActivity.java
package com.example.pulltorefresh;
import com.example.pulltorefresh.PullToRefreshListView.onRefreshListenner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
String[] datas = {"新闻","游戏","礼包","设置","购物","我的"};
private PullToRefreshListView pullToRefreshListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);
pullToRefreshListView.setAdapter(new pullRefreshAdapter());
//5.前端界面设置回调
pullToRefreshListView.setOnRefreshListenner(new onRefreshListenner() {
@Override
public void onRefresh() {
// 刷新数据
}
});
}
public class pullRefreshAdapter extends BaseAdapter{
private ViewHolder holder;
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return datas[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1 == null){
arg1 = View.inflate(getApplicationContext(), R.layout.text_layout, null);
holder = new ViewHolder();
holder.text = (TextView) arg1.findViewById(R.id.text);
arg1.setTag(holder);
}else{
arg1.getTag();
}
holder.text.setText(datas[arg0]);
return arg1;
}
}
public class ViewHolder{
TextView text;
}
}
主界面activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<com.example.pulltorefresh.PullToRefreshListView
android:id="@+id/pullToRefreshListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.pulltorefresh.PullToRefreshListView>
</LinearLayout>
适配器布局
text_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f00"
android:textSize="18dp" />
</LinearLayout>