自从开始学习的时候自己手动实现一个listview的上啦刷新下拉加载后就再也没自己手动写过,如今recycleview横行,但是这个作为基础,还是先回顾一下,自己就收到写了一个。
package com.example.administrator.testrefreshlisview;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
/**
* Created by Administrator on 2017/5/2.
*/
public class RefreshListview extends ListView implements AbsListView.OnScrollListener {
//是不是正在刷新或者加载
public int isWorking;
//上啦刷新下拉加载布局的高度,这里的上下用的一个布局。这个高度的作用是用以判断下拉或者上啦的距离是否超出这个高度,超出则表示需要上啦或者下拉
int topHegiht;
//根据滚动,判断是不是在顶部
int isTop;
//是否在底部的变量
int isBottom;
//顶部和底部view
View headView;
View bottomView;
//表示是下拉还是上啦
int isRefresh;
float Ypoint;
Context mContext;
boolean flag;
OnRefershListener onRefershListener;
TextView headText;
TextView footText;
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
}
public RefreshListview(Context context) {
super(context);
this.mContext = context;
this.addFooterView(LayoutInflater.from(context).inflate(R.layout.footview, null));
this.addHeaderView(LayoutInflater.from(context).inflate(R.layout.headview, null));
}
public RefreshListview(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
Ypoint = 0;
isRefresh = 0;
this.headView = LayoutInflater.from(context).inflate(R.layout.headview, null);
this.bottomView = LayoutInflater.from(context).inflate(R.layout.footview, null);
this.headView.measure(0, 0);
this.bottomView.measure(0, 0);
this.headView.setPadding(0, -this.headView.getMeasuredHeight(), 0, 0);
this.bottomView.setPadding(0, -this.bottomView.getMeasuredHeight(), 0, 0);
this.headText = (TextView) headView.findViewById(R.id.top);
this.footText = (TextView) bottomView.findViewById(R.id.top);
this.addHeaderView(headView);
this.addFooterView(bottomView);
topHegiht = this.headView.getMeasuredHeight();
//开始的时候滚动在顶部
isTop = 1;
isWorking = 0;
isBottom = 1;
}
public RefreshListview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.addFooterView(LayoutInflater.from(context).inflate(R.layout.footview, null));
this.addHeaderView(LayoutInflater.from(context).inflate(R.layout.headview, null));
}
public void setOnRefershListener(OnRefershListener onRefershListener) {
this.onRefershListener = onRefershListener;
this.setOnScrollListener(this);
}
/**
* @param view
* @param scrollState
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE://空闲状态
// Log.e("空闲状态", "空闲状态");
break;
case OnScrollListener.SCROLL_STATE_FLING://滚动状态
// Log.e("滚动状态", "滚动状态");
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://触摸后滚动
// Log.e("/触摸后滚动", "/触摸后滚动");
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//listview高度超出后滑动才会调用这个方法,在这里对超出屏幕的listview进行是否需要刷新和加载的判断
isTop = 0;
isBottom = 0;
if (firstVisibleItem == 0) {
isTop = 1;
// Log.e("在顶部", "在顶部");
}
if (firstVisibleItem + visibleItemCount == totalItemCount) {
isBottom = 1;
// Log.e("在底部", "在底部");
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
//按下
Ypoint = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
Log.e("是否在顶部和底部", "" + isTop + "底部" + isBottom);
float Y2 = ev.getY();
if (Y2 - Ypoint > topHegiht) {
//如果listview正在顶部,而且当前没有在刷新也没有在加载
if (isTop == 1 && isWorking == 0) {
isRefresh = 1;
this.headView.setPadding(0, 0, 0, 0);
Log.e("下拉", "下拉");
this.headText.setText("停止即可刷新");
} else {
}
} else if (Ypoint - Y2 > topHegiht) {
if (isBottom == 1 && isWorking == 0) {
isRefresh = 0;
this.bottomView.setPadding(0, 0, 0, 0);
this.footText.setText("停止即可加载");
} else {
//不在底部,但是在上啦
}
} else {
}
// Log.e("点击到的Y坐标", "" + Y2);
break;
case MotionEvent.ACTION_UP:
//表示手指已经抬起,在这里判断是否需要进行刷新和加载
if (isRefresh == 1 && isWorking == 0 && isTop == 1) {
isWorking = 1;
//这里需要加上刷新数据的逻辑
onRefershListener.onRefresh();
} else if (isRefresh == 0 && isWorking == 0 && isBottom == 1) {
isWorking = 1;
//这里需要加上加载数据的逻辑
onRefershListener.onLoadMore();
}
break;
default:
break;
}
return super.onTouchEvent(ev);
}
public void stopRefresh() {
ValueAnimator xiaoshi = ValueAnimator.ofInt(0, -topHegiht);
xiaoshi.setDuration(3000);
xiaoshi.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
headView.setPadding(0, (Integer) animation.getAnimatedValue(), 0, 0);
headText.setText("刷新中...");
}
});
xiaoshi.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
isWorking = 0;
headText.setText("下拉即可刷新");
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
xiaoshi.start();
}
public void stopLoadMore() {
ValueAnimator xiaoshi = ValueAnimator.ofInt(0, -topHegiht);
xiaoshi.setDuration(3000);
xiaoshi.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
bottomView.setPadding(0, (Integer) animation.getAnimatedValue(), 0, 0);
footText.setText("加载中...");
}
});
xiaoshi.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
isWorking = 0;
footText.setText("上拉加载更多");
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
xiaoshi.start();
}
interface OnRefershListener {
public void onRefresh();
public void onLoadMore();
}
}