自定义的下拉listView 控件
public class CustomListView extends ListView {
int height;
View view;
int downY;
//刷新的四种状态
final static int STATE_DONE = 1; //隐藏时
final static int STATE_PULL = 2; //下拉时
final static int STATE_RELEASE = 3; //松开时
final static int STATE_REFRESHING = 4; //正在刷新
//当前的状态
int currentState;
TextView tvState;
ImageView ivArrow;
ProgressBar progressBar;
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
currentState = STATE_DONE;
view = View.inflate(getContext(), R.layout.listview_header, null);
this.addHeaderView(view);
// getHeight,控件已经显示出来了
height = view.getHeight();
// Measured测量
// 0是一种测量方式
view.measure(0, 0);
height = view.getMeasuredHeight();
view.setPadding(0, -height, 0, 0);
// this.addFooterView(view);
tvState = (TextView) view.findViewById(R.id.tv_state);
ivArrow = (ImageView) view.findViewById(R.id.iv_arrow);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
// 按下
case MotionEvent.ACTION_DOWN:
if (currentState == STATE_DONE) {
downY = (int) ev.getY();
// 改变状态
currentState = STATE_PULL;
}
break;
// 移动
case MotionEvent.ACTION_MOVE:
if (currentState == STATE_PULL) {
int currentY = (int) ev.getY();
//控件到顶部的距离
int top = currentY - downY - height;
Log.i("计算top", currentY + ",downY=" + downY + ",height="
+ height + ",top=" + top);
view.setPadding(0, top, 0, 0);
if (currentY - downY > height) {
this.currentState = STATE_RELEASE;
tvState.setText("松开刷新");
}
}
break;
// 松开
case MotionEvent.ACTION_UP:
if (currentState == STATE_RELEASE) {
this.currentState = STATE_REFRESHING;
tvState.setText("正在刷新");
// 4,调接口实现类,调用别人代码
if (this.onRefresingListner != null) {
this.onRefresingListner.onRefresh(this);
}
ivArrow.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
}
break;
}
return super.onTouchEvent(ev);
}
// 3,接收接口的实现类
public void setOnRefreshingListener(OnRefresingListner onRefresingListner) {
this.onRefresingListner = onRefresingListner;
}
// 2,申明接口对象
OnRefresingListner onRefresingListner;
// 1 写个接中
interface OnRefresingListner {
public void onRefresh(CustomListView customListView);
}
//当刷新完成之后
public void refreshComplete() {
this.currentState = STATE_DONE;
view.setPadding(0, -height, 0, 0);
tvState.setText("下拉刷新");
progressBar.setVisibility(GONE);
ivArrow.setVisibility(VISIBLE);
}
}
在Activity 中
public class MainActivity extends Activity {
CustomListView customListView;
MyAdatper myAdatper;
ArrayList<String> list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 10; i++) {
list.add("data" + i);
}
myAdatper = new MyAdatper(this, list);
customListView = (CustomListView) findViewById(R.id.customListView);
customListView.setAdapter(myAdatper);
customListView.setOnRefreshingListener(new OnRefresingListner() {
@Override
public void onRefresh(final CustomListView customListView) {
//联网 启动工作线程
new Thread(){
public void run() {
try {
//开始连接
this.sleep(2000);
String data="联网取到的数据";
list.add(data);
//添加完数据 需要更新UI 在主线程中更新
runOnUiThread(new Runnable() {
@Override
public void run() {
customListView.refreshComplete();
//是1,就是主线程
int threadId=(int) Thread.currentThread().getId();
myAdatper.notifyDataSetChanged();
}
});
} catch (Exception e) {
// TODO: handle exception
}
};
}.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
layout 布局文件
<?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>