首先建立ListHeader的布局文件。用于存放下拉菜单
package com.example.myview;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class My_listview extends ListView implements OnScrollListener{
View header;
TextView tv;
ImageView iv;
ProgressBar probar;
Animation anim_downToUp;
Animation anim_UpToDown;
int headerHigth=0; //判断第一条被滑出的高度
public My_listview(Context context, AttributeSet attrs) {
super(context, attrs);
initAnimation();//执行动画
header=(View)LayoutInflater.from(context).inflate(R.layout.list_header, null);
tv=(TextView)header.findViewById(R.id.mytitle);
iv=(ImageView)header.findViewById(R.id.img);
probar=(ProgressBar)header.findViewById(R.id.progressbar);
probar.setVisibility(View.INVISIBLE); //设置滚动条初始状态为不可见
addHeaderView(header);
setSelection(1);
headerHigth=getHeaderViewHeight();//获取下拉菜单的高度
setOnScrollListener(this);
}
//获取下拉菜单的高度
private int getHeaderViewHeight() {
android.view.ViewGroup.LayoutParams params=header.getLayoutParams();
if(params==null){
params=new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
}
int with=MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.EXACTLY);
int height=MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY);
header.measure(with, height);
Log.i("==================", " 高度"+header.getMeasuredHeight());
return header.getMeasuredHeight();
}
int myscrollState;//获取滑动的方式
boolean flag=true;//判断下滑菜单状态
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
myscrollState=scrollState;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
//判断是否是手指滑动触发的事件
if(myscrollState==SCROLL_STATE_TOUCH_SCROLL){
//判断是否滑动到第一条
if(firstVisibleItem==0){
//getBottom获取header控件的下标
if(header.getBottom()
iv.setVisibility(View.VISIBLE);
probar.setVisibility(view.INVISIBLE);
tv.setText(" 下拉刷新...... ");
iv.startAnimation(anim_UpToDown);
flag=false;
}
else if(header.getBottom() >=headerHigth){
iv.setVisibility(View.VISIBLE);
probar.setVisibility(view.INVISIBLE);
tv.setText(" 释放开始刷新...... ");
iv.startAnimation(anim_downToUp);
flag=true;
}
}
}
}
//设置动画效果
private void initAnimation() {
anim_downToUp = new RotateAnimation(0, -180,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim_downToUp.setDuration(300);
anim_downToUp.setFillAfter(true);
anim_UpToDown = new RotateAnimation(-180, 0,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim_UpToDown.setDuration(300);
anim_UpToDown.setFillAfter(true);
}
//当手指抬起时的事件
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(ev.getAction()==MotionEvent.ACTION_UP){
//下拉菜单被完全拉出时
if(flag){
iv.clearAnimation();//清除动画
iv.setVisibility(View.INVISIBLE);
probar.setVisibility(View.VISIBLE);
tv.setText("正在刷新。。。");
if(listener!=null){
listener.onRefresh();
}
}
else{
iv.clearAnimation();
setSelection(1);
}
}
return super.onTouchEvent(ev);
}
public void refreshFinish() {
invalidateViews(); //刷新页面,即刷新listview中所有的子Views
iv.clearAnimation();
setSelection(1);
}
OnRefreshListener listener;
public void setListener(OnRefreshListener listener) {
this.listener = listener;
}
//设置抽象类 用于接口回调。添加事件
public interface OnRefreshListener{
void onRefresh();
}
@Override
public void setAdapter(ListAdapter adapter) {
// TODO Auto-generated method stub
super.setAdapter(adapter);
setSelection(1);
}
}
第三步在主布局文件中添加自定义ListView的控件
fill_parent"
/>
package com.example.myview;
import java.util.ArrayList;
import java.util.List;
import com.example.myview.My_listview.OnRefreshListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
My_listview listview;
Listlist=new ArrayList();
ArrayAdapter< String >adapter;
int num=0;//新添加的数据
Handler handler = new Handler();//延迟加载数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
for(int i=0;i<20;i++){
list.add("item"+i);
}
adapter=new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,list);
listview.setAdapter(adapter);
listview.setListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// TODO Auto-generated method stub
Log.i("=====", "============开始下拉刷新");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
num++;
list.add(0,"新添加的数据"+num);
//刷新页面
listview.refreshFinish();
}
}, 2000);
}
});
}
}