ListView下拉刷新
1.添加提示界面,即ListView的header头布局
2.监听ListView滚动事件,即onScrollListener()
3.监听ListView onTouch()事件
4.加载最新数据
小说明:
VISIBLE:设置控件可见 (保留View所占空间) INVISIBLE :设置控件不可见(保留View所占空间) GONE:设置控件隐藏(不保留View所占空间 )
1.添加提示界面,即ListView的header头布局
A.初始化界面,添加顶部的布局文件到ListView
问题:添加header头布局文件如何显示和隐藏
思考:使用INVISIBLE 和GONE没有变化的过程,所以不可行
答案:将顶部布局的上边距设置成其高度的负值,即可达到隐藏heaer的效果,注意要通知父布局其高度值,否则其高度值为0,仍然无法隐藏
代码:
/**
* 初始化界面,添加顶部布局文件到listview
* @param context
*/
private void initView(Context context){
LayoutInflater inflater=LayoutInflater.from(context);
header=inflater.inflate(R.layout.header_layout, null);
measureView(header);//1.初始化header的宽度和高度
headerHeight = header.getMeasuredHeight();
Log.i("ReFlashListView", "headerHeight = " + headerHeight);
topPadding(-headerHeight);//2.设置距离顶部的高度为header的负值,达到隐藏heaer的效果
this.addHeaderView(header);
}
/**
* 设置header 布局 上边距;
* @param topPadding
*/
private void topPadding(int topPadding) {
Log.i("ReFlashListView", "header.getPaddingTop() = " + header.getPaddingTop());
header.setPadding(header.getPaddingLeft(), topPadding,
header.getPaddingRight(), header.getPaddingBottom());
header.invalidate();
}
/**
* 通知父布局,view占用的宽高
* @param view
*/
private void measureView(View view) {
ViewGroup.LayoutParams p=view.getLayoutParams();
if(p==null){
p=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width=ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height,tempHeight;
tempHeight=p.height;
if(tempHeight>0){
height=MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
}else {
height=MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
}
隐藏header需要注意几个地方:
1.首先需要通过measureView(View view)初始化header的宽度和高度
2.设置header布局上边距注意设置上边距是topPadding而不是header.getPaddingTop()
正确的写法:
header.setPadding(header.getPaddingLeft(), topPadding,
header.getPaddingRight(), header.getPaddingBottom());
错误的写法:
header.setPadding(header.getPaddingLeft(), header.getPaddingTop(),
header.getPaddingRight(), header.getPaddingBottom());
说明:本人不小心,写成错误写法,一直隐藏不了header,header.getPaddingTop()值是0
2.监听ListView滚动事件,即onScrollListener()
3.监听ListView onTouch()事件
分析:下拉刷新,是在View的最顶部进行滑动事件的触发的,我们所做的动作是按下,移动,抬起
按下时:我们需要一个标志位,记录当前是在ListView最顶端按下的,同时需要一个变量记录按下时Y值
移动时:我们需要定义几个常量区分当前状态:正常状态、提示下拉刷新状态、提示松开释放的状态、提示正在刷新状态,标记位为true;正常状态:正常状态-->提示下拉刷新状态:最后移动的位置Y值-按下时Y值大于0;提示下拉刷新状态:提示下拉刷新状态-->提示松开释放状态:最后移动的位置Y值-按下时Y值大于一定的高度,并且当前的状态是正在滚动的状态;提示松开释放状态:提示松开释放-->正常状态:当最后移动的位置Y值-按下时Y值小于等于0,标记位为false;提示松开释放状态-->提示下拉刷新状态,最后移动位置的Y值-按下时Y值小于一定的高度;由于header是在移动的时候一点点出现的,所以要不断设置topPadding;
抬起时:判断当前状态,如果当前状态时松开释放状态,状态就改变为提示正在刷新状态,加载最新数据;如果当前状态时下拉刷新状态,不加载数据,状态变为正常状态,标记为false
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (firstVisibleItem == 0) {
isRemark = true;//按下标记置true
startY = (int) ev.getY();//按下时当前的Y值
}
break;
case MotionEvent.ACTION_MOVE:
onMove(ev);
break;
case MotionEvent.ACTION_UP:
if (state == RELESE) {//当前状态为提示释放刷新
state = REFLASHING;//改变状态为正在刷新
// 加载最新数据;
reflashViewByState();
iReflashListener.onReflash();//加载刷新数据完成,通知更新界面
} else if (state == PULL) {//当前状态为提示下拉刷新
state = NONE;//改变当前状态为正常状态
isRemark = false;//按下标记置为false
reflashViewByState();//更新界面状态
}
break;
}
return super.onTouchEvent(ev);
}
/**
* 判断移动过程操作;
*
* @param ev
*/
private void onMove(MotionEvent ev) {
if (!isRemark) {
return;
}
int tempY = (int) ev.getY();
int space = tempY - startY;
int topPadding = space - headerHeight;
switch (state) {
case NONE:
if (space > 0) {//移动位置大于0,即为下拉状态
state = PULL;//改变状态为下拉刷新状态
reflashViewByState();//更新界面
}
break;
case PULL:
topPadding(topPadding);//设置上边距
if (space > headerHeight + 30
&& scrollState == SCROLL_STATE_TOUCH_SCROLL) {
state = RELESE;//移动位置大于一定的高度,改变状态为松开刷新状态
reflashViewByState();//更新界面
}
break;
case RELESE:
topPadding(topPadding);//设置上边距
if (space < headerHeight + 30) {
state = PULL;//移动位置小于一定的高度,改变状态为下拉刷新状态
reflashViewByState();//更新界面
} else if (space <= 0) {
state = NONE;//移动位置小于0,改变状态为正常状态
isRemark = false;//标记为置为false
reflashViewByState();//更新界面
}
break;
}
}
根据当前的状态更新显示界面:
/**
* 根据当前状态,改变界面显示;
*/
private void reflashViewByState() {
TextView tip = (TextView) header.findViewById(R.id.tip);
ImageView arrow = (ImageView) header.findViewById(R.id.arrow);
ProgressBar progress = (ProgressBar) header.findViewById(R.id.progress);
RotateAnimation anim = new RotateAnimation(0, 180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(500);
anim.setFillAfter(true);
RotateAnimation anim1 = new RotateAnimation(180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
anim1.setDuration(500);
anim1.setFillAfter(true);
switch (state) {
case NONE:
arrow.clearAnimation();
topPadding(-headerHeight);
break;
case PULL:
arrow.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
tip.setText("下拉可以刷新!");
arrow.clearAnimation();
arrow.setAnimation(anim1);
break;
case RELESE:
arrow.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
tip.setText("松开可以刷新!");
arrow.clearAnimation();
arrow.setAnimation(anim);
break;
case REFLASHING:
topPadding(50);
arrow.setVisibility(View.GONE);
progress.setVisibility(View.VISIBLE);
tip.setText("正在刷新...");
arrow.clearAnimation();
break;
}
}
/**
* 获取完数据;
*/
public void reflashComplete() {
state = NONE;
isRemark = false;
reflashViewByState();
TextView lastupdatetime = (TextView) header
.findViewById(R.id.lastupdate_time);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
Date date = new Date(System.currentTimeMillis());
String time = format.format(date);
lastupdatetime.setText(time);
}
4.加载最新数据
更新数据到主界面:在自定义listview写接口,在MainActivity中实现接口中的方法
public void setInterface(IReflashListener iReflashListener){
this.iReflashListener = iReflashListener;
}
/**
* 刷新数据接口
* @author Administrator
*/
public interface IReflashListener{
public void onReflash();
}
在MainActivity.java中实现接口:
@Override
public void onReflash() {
// TODO Auto-generated method stub
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//获取最新数据
setReflashData();
//通知界面显示
showList(apk_list);
//通知listview 刷新数据完毕;
listview.reflashComplete();
}
}, 2000);
}
注意:这里为了看到刷新过程效果,加了handler进行延时,实际的操作是不需要handler延时,直接刷新数据的
总结:ListView下拉刷新,难点和亮点是通过接口回调的方法实现,界面同步更新数据
在学习下拉刷新的时候,遇到了几个问题和大家分享一下:
1.刷新数据的时候,header离顶部的距离,可以通过topPadding(30);
2.刷新数据的时候,animtop.setDuration(time);当time的设定的值较大时候,会看到箭头变化的比较慢
3.setReflashData();刷新数据的时候,注意不要重新new对象实例,否则数据无法叠加在一起,如果发现数据无法正常加载,可以看看是否在加载数据的时候,不停的在new新的对象实例
代码下载路径:
1.listview小demo:http://pan.baidu.com/s/1bnpxc4Z
2.listview下拉刷新代码:http://pan.baidu.com/s/1sjqW3nB