public class MainActivity extends Activity {
private PullToRefreshListView list_view;
private ArrayList<String> datas;
private int i;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_view = (PullToRefreshListView) findViewById(R.id.list_view);
datas = new ArrayList<String>();
// 模拟数据
for (int i = 0; i < 10; i++) {
datas.add(new String("我明天将会拥有" + i * 10 + "千万"));
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
list_view.setAdapter(adapter);
list_view.setOnRefreshingListener(new OnRefreshingListener() {
@Override
public void onRefreshing() {
refreshData();
}
@Override
public void onLoadingMore() {
loadMore();
}
});
}
/**
* 模拟开子线程去加载更多的数据
*/
protected void loadMore() {
Toast.makeText(this, "正在加载更多。。。", Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
datas.add("我是加载更多出来的数据 ");
adapter.notifyDataSetChanged();
list_view.loadMoreComplete();
}
}, 3000);
}
/**
* 模拟开子线程刷新数据
*/
protected void refreshData() {
Toast.makeText(this, "正在刷新数据。。。", Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
datas.add(0, "我是刷新出来的数据 " + (++i));
adapter.notifyDataSetChanged();
list_view.onRefreshComplete();
}
}, 3000);
}
}
private float downY;
private View headView;
private int headViewHeight;
/** 下拉刷新状态 */
private static final int STATE_PULL_TO_REFRESH = 0;
/** 松开刷新状态 */
private static final int STATE_RELEASE_TO_REFRESH = 1;
/** 正在刷新状态 */
private static final int STATE_REFRESHING = 2;
/** 当前状态(默认是下拉刷新状态 )*/
private int currentState = STATE_PULL_TO_REFRESH;
/** 是否有移动 */
private boolean hasMove;
private TextView tv_state;
private ImageView iv_arrow;
/** 往上转:0 ~ -180 */
private RotateAnimation upAnim;
/** 往下转:-180 ~ -360 */
private RotateAnimation downAnim;
private ProgressBar progressBar;
private OnRefreshingListener listener;
private View footerView;
private int footerViewHeight;
private boolean loadingMore;
public PullToRefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
initHeadView();
initFooterView();
}
private void initFooterView() {
footerView = View.inflate(getContext(), R.layout.foot_view, null);
footerView.measure(0, 0);// 让系统主动去测量这个View
footerViewHeight = footerView.getMeasuredHeight();
hideFooterView();
addFooterView(footerView);
setOnScrollListener(mOnScrollListener);
}
OnScrollListener mOnScrollListener = new OnScrollListener() {
/** 滑动状态发生改变 */
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 当处于空闲状态的时候,并且最后一条可见的item是我们ListView的最后一条数据,把FooterView显示出来
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE // 处于空闲状态了
&& getLastVisiblePosition() == getCount() - 1// 最后一条可见的item的索引是ListView的最后一条数据的索引
&& !loadingMore) {// 没有正在加载
showFooterView();
setSelection(getCount() - 1);// 选择ListView的最后一个Item,它会自动滚动ListView
loadingMore = true;
if (listener != null) {
listener.onLoadingMore();
}
System.out.println("正在加载更多。。。");
}
}
/** 正在滑动 */
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
};
private void hideFooterView() {
setFooterViewPaddingTop(-footerViewHeight);
}
private void showFooterView() {
setFooterViewPaddingTop(0);
}
private void setFooterViewPaddingTop(int top) {
footerView.setPadding(0, top, 0, 0);
}
private void initHeadView() {
headView = View.inflate(getContext(), R.layout.head_view, null);
tv_state = (TextView) headView.findViewById(R.id.tv_state);
iv_arrow = (ImageView) headView.findViewById(R.id.iv_arrow);
progressBar = (ProgressBar) headView.findViewById(R.id.progress_bar);
headView.measure(0, 0);// 让系统主动去测量这个View
headViewHeight = headView.getMeasuredHeight();
hideHeadView();
addHeaderView(headView);
initAnimator();
}
private void initAnimator() {
upAnim = createRotateAnimator(0, -180);
downAnim = createRotateAnimator(-180, -360);
}
/**
* 创建一个旋转动画
* @param fromDegrees 旋转的开始角度
* @param toDegrees 旋转的结束角度
*/
private RotateAnimation createRotateAnimator(float fromDegrees, float toDegrees) {
int pivotXType = RotateAnimation.RELATIVE_TO_SELF;// 旋转点x方向的参数物
int pivotYType = RotateAnimation.RELATIVE_TO_SELF;// 旋转点y方向的参数物
float pivotXValue = 0.5f;// 旋转点x方向的位置
float pivotYValue = 0.5f;// 旋转点y方向的位置
RotateAnimation ra = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue);
ra.setFillAfter(true);// 让动画保持在结束的样子
ra.setDuration(300);
return ra;
}
/** 隐藏HeadView */
private void hideHeadView() {
setHeadViewPaddingTop(-headViewHeight);
}
/** 显示HeadView */
private void showHeadView() {
setHeadViewPaddingTop(0);
}
/** 设置HeadView的paddingTop */
private void setHeadViewPaddingTop(int top) {
headView.setPadding(0, top, 0, 0);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
float distanceY = ev.getY() - downY;
// 当往下滑动,并且第一条可以的item是索引为第1条的数据,这个时候才需要显示HeadView
if (distanceY > 0 && getFirstVisiblePosition() == 0) {
hasMove = true;
// 下拉显示HeadView的原理:-HeadView.height + 往下移动的距离
int paddingTop = (int) (-headViewHeight + distanceY);
setHeadViewPaddingTop(paddingTop);
if (paddingTop < 0 && currentState != STATE_PULL_TO_REFRESH) {
// 如果paddingTop小于0,说明HeadView没有完全显示,进入下拉刷新状态
currentState = STATE_PULL_TO_REFRESH;
tv_state.setText("下拉刷新");
showArrow(true);
iv_arrow.startAnimation(downAnim);
} else if (paddingTop >= 0 && currentState != STATE_RELEASE_TO_REFRESH) {
// 如果如果paddingTop大于或者等于0,说明HeadView完全显示出来,进入松开刷新状态
currentState = STATE_RELEASE_TO_REFRESH;
tv_state.setText("松开刷新");
showArrow(true);
iv_arrow.startAnimation(upAnim);
}
return true;
}
break;
case MotionEvent.ACTION_UP:
if (hasMove) {
// 如果有移动HeadView的操作才需要做下面的事件
if (currentState == STATE_RELEASE_TO_REFRESH) {
// 如果当前状态是松开刷新状态,并且抬起了手,则进入正在刷新状态
currentState = STATE_REFRESHING;
tv_state.setText("正在刷新");
if (listener != null) {
listener.onRefreshing();
}
showArrow(false);
showHeadView();
} else if (currentState == STATE_PULL_TO_REFRESH) {
hideHeadView();
}
}
break;
}
return super.onTouchEvent(ev);
}
/**
* 设置是否显示箭头,如果显示了箭头,则隐藏progressBar,如果不显示箭头,则显示progressBar
* @param isShowArrow 如果为true则显示箭头
*/
private void showArrow(boolean isShowArrow) {
iv_arrow.setVisibility(isShowArrow ? View.VISIBLE : View.INVISIBLE);
progressBar.setVisibility(!isShowArrow ? View.VISIBLE : View.INVISIBLE);
if (!isShowArrow) {
// 如果是要显示progressBar的话则把箭头的动画消除
iv_arrow.clearAnimation();
}
}
/**
* 设置正在刷新的监听器
* @param listener
*/
public void setOnRefreshingListener(OnRefreshingListener listener) {
this.listener = listener;
}
public interface OnRefreshingListener {
/** 正在刷新的时候会回调这个方法 */
void onRefreshing();
/** 加载更多的时候会回调这个方法 */
void onLoadingMore();
}
/** 数据刷新完成 了 */
public void onRefreshComplete() {
hideHeadView();
currentState = STATE_PULL_TO_REFRESH;
}
/**
* 加载更多完成了
*/
public void loadMoreComplete() {
hideFooterView();
loadingMore = false;
}
}