Android学习笔记:实现下拉刷新的ListView

             ListView的下拉刷新及上拉加载更多数据是我们开发中通常要实现的功能,开源项目中有很多的上下拉加载刷新的ListView可直接拿来使用,这几天刚好学习了下刷新的实现方式,把学习的资料做个记录:

       实现的简单效果图片:这是下拉时显示,文字可能有些不一样。向上返回如图:。此时放开手后,就有一个圆形进度条加载状态,加载完成后HeaderView部分(包括上面的图片,及加载条都是隐藏)。

  实现的代码:

/**
 * 自定义可以下拉刷新的ListView
 * 
 * @description:
 * 下拉刷新实现方式分析:
 * 1,我们要给ListView添加头部(HeaderView)实现下拉加载的提示效果
 * 2,需要对ListView进行onScrollListener监听,来判断当前是否显示在listview的最顶部
 * 3,ListView的下拉加载等界面是随手势滑动过程不断变化的,所以要监听onTouch事件,来改变当前的状态及界面显示
 * 4,最后就是根据当前的状态来实现加载数据
 * @date 2015-10-27 下午12:16:59
 */
public class PullDownFreshListView extends ListView implements OnScrollListener {
/* 下拉的ListView头部* */
private View mHeaderView;
/* 头部的高度* */
private int headerHeight;
/* 当前每一个可见的item的位置* */
private int mFirstVisibleItem;
/* 当前的滚动状态* */
private int mScrollState;
/* 设置下拉起始的y值* */
private int startY;
/* 是否是在ListView顶端按下的(下拉的)* */
private boolean isRemark = false;
/* 定义当前ListView所处的状态* */
private int state;
/* 定义状态:正常* */
private static final int NORMAL = 1;
/* 定义状态:下拉状态* */
private static final int PULL = 2;
/* 定义状态:下拉释放状态* */
private static final int RELEASE = 3;
/* 定义状态:刷新中状态* */
private static final int FRESHING = 4;
/* 刷新完成回调接口* */
private IPullDownFreshListener freshListener;


public void setFreshListener(IPullDownFreshListener freshListener) {
this.freshListener = freshListener;
}


public PullDownFreshListView(Context context) {
super(context);
initHeaderView(context);
}


public PullDownFreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderView(context);
}


public PullDownFreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initHeaderView(context);
}


/**
* 初始化头部headerView
* @description:
* @date 2015-10-27 下午1:03:32
*/
private void initHeaderView(Context context) {
LayoutInflater mInflater = LayoutInflater.from(context);
mHeaderView = mInflater.inflate(R.layout.fresh_header, null);
measureHeaderView(mHeaderView);
headerHeight = mHeaderView.getMeasuredHeight();
topPadding(-headerHeight);// 设置pading为header高度的负责来初始隐藏headerView
this.addHeaderView(mHeaderView);
}


/**
* 设置ListView的padding
* @description:
* @date 2015-10-27 下午1:25:45
*/
private void topPadding(int top) {
setPadding(mHeaderView.getPaddingLeft(), top, mHeaderView.getPaddingRight(), mHeaderView.getPaddingBottom());
mHeaderView.invalidate();
}


/**
* 测量view在父容器中占用的高度
* @description:
* @date 2015-10-27 下午1:17:39
*/
private void measureHeaderView(View view) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp == null) {
lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width = ViewGroup.getChildMeasureSpec(0, 0, lp.width);
int height;
int tempHeight = lp.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
}
else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
}


@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.mFirstVisibleItem = firstVisibleItem;// 获取当前可见item的位置
}


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.mScrollState = scrollState;
}


@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mFirstVisibleItem == 0) {
isRemark = true;
startY = (int) ev.getY();// 按下手指时获取到初始的位置y值
state = NORMAL;
}
break;
case MotionEvent.ACTION_MOVE:
onMove(ev);
break;
case MotionEvent.ACTION_UP:
if (state == RELEASE) {
state = FRESHING;
// 加载最新数据
reFreshViewsByState();
if (freshListener != null) {
freshListener.onFresh();
}
}
else if (state == PULL) {
state = NORMAL;
isRemark = false;
reFreshViewsByState();
}
break;
}
return super.onTouchEvent(ev);
}


/**
* 下拉过程中的逻辑处理
* @description:
* @date 2015-10-27 下午1:34:13
*/
private void onMove(MotionEvent ev) {
if (!isRemark) { return; }
int tempY = (int) ev.getY();
int spaceY = tempY - startY;// 获取到滑动(拉动)产生的距离
int topPadding = tempY - startY;
switch (state) {
case NORMAL:
if (spaceY > 0) {
state = PULL;
}
break;
case PULL:
topPadding(topPadding);
if (spaceY > headerHeight + 30 && mScrollState == SCROLL_STATE_TOUCH_SCROLL) {
state = RELEASE;
}
break;
case RELEASE:
if (spaceY < headerHeight + 30) {
state = PULL;
}
else if (spaceY <= 0) {
state = NORMAL;
}
break;
case FRESHING:
break;
}


}


/**
* 刷新结束的处理
* @description:
* @date 2015-10-27 下午1:50:53
*/
@SuppressLint("SimpleDateFormat")
public void freshCompelete() {
state = NORMAL;
isRemark = false;
reFreshViewsByState();
TextView freshTimeTv = (TextView) mHeaderView.findViewById(R.id.item_time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 mm:dd:ss");
Date now = new Date(System.currentTimeMillis());
freshTimeTv.setText(sdf.format(now));// 设置刷新的时间显示
}


/**
* 根据当前 状态 来显示headerView
* @description:
* @date 2015-10-27 下午1:51:45
*/
private void reFreshViewsByState() {
TextView tip = (TextView) mHeaderView.findViewById(R.id.item_title);
ImageView freshIv = (ImageView) mHeaderView.findViewById(R.id.item_iv);
ProgressBar pBar = (ProgressBar) mHeaderView.findViewById(R.id.item_progress);
// 设置下拉时的图片动画
RotateAnimation downAnim = new RotateAnimation(0, 180, 0.5f, 0.5f);
downAnim.setDuration(500);
downAnim.setFillAfter(true);
RotateAnimation upAnim = new RotateAnimation(180, 0, 0.5f, 0.5f);
upAnim.setDuration(500);
upAnim.setFillAfter(true);
switch (state) {
case NORMAL:
topPadding(-headerHeight);
break;
case PULL:
freshIv.setVisibility(View.VISIBLE);
pBar.setVisibility(View.GONE);
tip.setText("下拉可以刷新!");
freshIv.clearAnimation();
freshIv.setAnimation(downAnim);
break;
case RELEASE:
freshIv.setVisibility(View.VISIBLE);
pBar.setVisibility(View.GONE);
tip.setText("松开可以刷新!");
freshIv.clearAnimation();
freshIv.setAnimation(upAnim);
break;
case FRESHING:
topPadding(50);
freshIv.setVisibility(View.GONE);
pBar.setVisibility(View.VISIBLE);
tip.setText("正在刷新!");
freshIv.clearAnimation();
break;
}
}


/**
* 增加一个刷新完成的回调接口
* @description:
* @date 2015-10-28 上午9:06:05
*/
public interface IPullDownFreshListener {
void onFresh();// 刷新方法
}
}

--------------在Activity中使用很方便,同通常的ListView是一样的,在布局文件中引入我们自定义ListView--------------------------------------------------------

public class MainActivity extends Activity {
private PullDownFreshListView mDownFreshListView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDownFreshListView = (PullDownFreshListView) findViewById(R.id.fresh_listview);

                   //创建适配器及模拟数据的操作就省略了.....
mDownFreshListView.setFreshListener(new IPullDownFreshListener() {


@Override
public void onFresh() {
// 加载数据
// 刷新界面
mDownFreshListView.freshCompelete();// 调用 刷新完成
}
});
}
}

你可能感兴趣的:(Android开发,ListView,ListView下拉刷新)