android下拉列表listview,android下拉刷新ListView的介绍和实现代码

package com.chenzong;

import java.util.Calendar;

import com.doall.pushrefreshlist.R;

import android.content.Context;

import android.os.Handler;

import android.os.Message;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.view.MotionEvent;

import android.view.View;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.ListView;

import android.widget.TextView;

public class PushRefreshList extends ListView implements RefreshOperation{

private int header_layout=R.layout.chenzong_push_refresh_header;

//表头文件

private int arrow_down=R.drawable.arrow_down;

//箭头往下的资源

private int arrow_up=R.drawable.arrow_up;

//箭头往上的资源

private int img=R.id.push_refresh_header_img;

//显示箭头的控件id

private int pb=R.id.push_refresh_header_pb;

//刷新时的进度条

private int startPoint=0;

//触摸的起始点

private RefreshOperation refresh;

//刷新行为的对象

private Animation animation=null;

private Context context;

private View headerView;

private int minPushHeight;

private final String TAG="pushRefresh";

public PushRefreshList(Context cotext, AttributeSet attrs) {

super(context, attrs);

View empty=new View(context);

//判断是否到列表的顶端,通常要用到this.getFirstVisiblePosition(),这里创建一个高度的为零View,加到headerView和数据之间

this.addHeaderView(empty);

LayoutInflater inflater=LayoutInflater.from(context);

headerView=inflater.inflate(header_layout, null);

this.addHeaderView(headerView);

this.setRefreshOperation(this);

this.context=context;

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

this.minPushHeight=headerView.getMeasuredHeight();

//获取下拉刷新的触发高度

super.onLayout(changed, l, t, r, b);

}

private boolean canHandleEvent(int dy)

{

return (dy<0&&this.getFirstVisiblePosition()==0&&!isPbVisible());

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

int action=ev.getAction();

switch(action)

{

case MotionEvent.ACTION_DOWN:

startPoint=(int)ev.getY();

break;

case MotionEvent.ACTION_MOVE:

int dy=startPoint-(int)ev.getY();

if(canHandleEvent(dy))

{

if(animation==null)

{

if(Math.abs(this.getScrollY())>=this.minPushHeight)

{

animation=AnimationUtils.loadAnimation(context, R.anim.arrow_rotate);

View mView=headerView.findViewById(img);

mView.startAnimation(animation);

this.setScrollbarFadingEnabled(true);

}

}

this.scrollTo(0,dy/2);

return true;

}

break;

case MotionEvent.ACTION_UP:

this.setScrollbarFadingEnabled(false);

if(animation!=null)

{

setImgBackgroundUp();

switchCompent(View.INVISIBLE,View.VISIBLE);

this.scrollTo(0,-minPushHeight);

PushRefreshList.this.refresh.OnRefreshStart();

new Thread(mRunnable).start();

animation=null;

}

else

this.scrollTo(0,0);

break;

}

return super.onTouchEvent(ev);

}

private Runnable mRunnable=new Runnable()

{

@Override

public void run() {

PushRefreshList.this.refresh.OnRefreshing();

mHandler.obtainMessage().sendToTarget();

}

};

private Handler mHandler=new Handler()

{

@Override

public void handleMessage(Message msg) {

PushRefreshList.this.refresh.OnRefreshEnd();

PushRefreshList.this.scrollTo(0, 0);

PushRefreshList.this.setImgBackgroundDown();

PushRefreshList.this.switchCompent(View.VISIBLE, View.GONE);

TextView tv=(TextView)headerView.findViewById(R.id.push_refresh_header_date);

tv.setText(this.getDateStr());

}

private String getDateStr()

{

Calendar ca=Calendar.getInstance();

int year=ca.get(Calendar.YEAR);

int month=ca.get(Calendar.MONTH);

int date=ca.get(Calendar.DATE);

int hour=ca.get(Calendar.HOUR);

int mintes=ca.get(Calendar.MINUTE);

int second=ca.get(Calendar.SECOND);

return year+"-"+(month+1)+"-"+date+" "+hour+":"+mintes+":"+second;

}

};

private void switchCompent(int imgStatus,int pbStatus)

{

View img=headerView.findViewById(R.id.push_refresh_header_img);

img.clearAnimation();

//执行了动画的控件如果不调用clearAnimation,setVisibility(View.GONE)会失效

img.setVisibility(imgStatus);

headerView.findViewById(R.id.push_refresh_header_pb).setVisibility(pbStatus);

}

private boolean isPbVisible()

{

return View.VISIBLE==headerView.findViewById(R.id.push_refresh_header_pb).getVisibility();

}

private void setImgBackgroundUp()

{

View mView=headerView.findViewById(this.img);

mView.setBackgroundResource(arrow_up);

}

private void setImgBackgroundDown()

{

View mView=headerView.findViewById(this.img);

mView.setBackgroundResource(arrow_down);

}

public void setRefreshOperation(RefreshOperation refresh)

{

this.refresh=refresh;

}

@Override

public void OnRefreshStart() {

}

@Override

public void OnRefreshing() {

}

@Override

public void OnRefreshEnd() {

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值