第一次刷新:
第二次刷新:
ListView上面的header文件佈局:
<?xml version="1.0" encoding="utf-8"?>
<!-- ListView的頭部 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000" >
<!-- 內容 -->
<RelativeLayout
android:id="@+id/head_contentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp" >
<!-- 箭頭圖像、進度條 -->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<!-- 箭頭 -->
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/arrow" />
<!-- 進度條 -->
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
<!-- 提示、最近更新 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
<!-- 提示 -->
<TextView
android:id="@+id/tv_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉刷新"
android:textColor="#FF000000"
android:textSize="20sp" />
<!-- 最近更新 -->
<TextView
android:id="@+id/tv_updateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFF0000"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
自定義ListView:
package com.example.customlistview;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class CustomListView extends ListView{
View header;
TextView tvState;
TextView upDateTime;
ProgressBar progressBar;
int viewHeight;
ImageView ivArror;
int downY;
byte currentState;
public static final byte STATE_DONE=0;
public static final byte STATE_PULL=1;
public static final byte STATE_RELEASE=2;
public static final byte STATE_REFRESHING=3;
String time="";
OnRefreshListener onRefreshListener;
public void setOnRefershListener(OnRefreshListener onRefreshListener){
this.onRefreshListener=onRefreshListener;
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
currentState=STATE_DONE;
header=View.inflate(context, R.layout.list_header,null);
tvState=(TextView) header.findViewById(R.id.tv_state);
progressBar=(ProgressBar) header.findViewById(R.id.progressBar);
ivArror=(ImageView) header.findViewById(R.id.iv_arrow);
upDateTime=(TextView) header.findViewById(R.id.tv_updateTime);
this.addHeaderView(header);
header.measure(0, 0);
viewHeight=header.getMeasuredHeight();
//header.setPadding(left, top, right, bottom);
header.setPadding(0, -viewHeight, 0, 0);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action=ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
if(this.currentState==STATE_DONE){
this.currentState=STATE_PULL;
this.downY=(int) ev.getY();
}
break;
case MotionEvent.ACTION_MOVE:
if(currentState==STATE_PULL){
int moveY=(int) ev.getY();
int top=-viewHeight+moveY-downY;
header.setPadding(0, top, 0, 0);
if(moveY-downY>viewHeight){
this.currentState=STATE_RELEASE;
tvState.setText("鬆開刷新");
if("".equals(time)){
upDateTime.setText(time);
}else{
upDateTime.setText("上次刷新:"+time);
}
}
}
break;
case MotionEvent.ACTION_UP:
if(currentState==STATE_RELEASE){
currentState=STATE_REFRESHING;
tvState.setText("刷新中");
ivArror.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
if (onRefreshListener != null) {
onRefreshListener.onRefresh(this);
}
}
break;
}
return super.onTouchEvent(ev);
}
public String getDate(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String date=sdf.format(new Date());
return date;
}
public void refreshComplete(){
progressBar.setVisibility(View.GONE);
ivArror.setVisibility(View.VISIBLE);
tvState.setText("下拉刷新");
time=getDate();
upDateTime.setText("本次刷新:"+time);
header.setPadding(0 , -viewHeight, 0, 0);
currentState=STATE_DONE;
}
interface OnRefreshListener{
public void onRefresh(CustomListView customListView);
}
}
MainActivity:
public class MainActivity extends Activity {
CustomListView customListView;
ArrayList<String> list=new ArrayList<String>();
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
}
private void setUpView() {
customListView=(CustomListView) findViewById(R.id.customView);
list.add("dadwa");list.add("dadfewwa");
list.add("dfwadwa");list.add("dafewedwa");
list.add("datrdwa");list.add("da4t3dwa");
adapter=new MyAdapter(this, list);
customListView.setAdapter(adapter);
customListView.setOnRefershListener(new OnRefreshListener() {
@Override
public void onRefresh(final CustomListView customListView) {
//模擬聯網刷新
new Thread(){
public void run() {
try {
this.currentThread().sleep(5000);
for(int i=0;i<10;i++){
list.add(""+i);
}
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
customListView.refreshComplete();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
});
}