package com.example.lsf.myapplication;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Date;
/**
* Created by lsf on 2018/2/7.
*/
* Created by lsf on 2018/2/7.
*/
public class LoadListView extends ListView implements AbsListView.OnScrollListener {
int headerHeight;// 顶部布局文件的高度;
int scrollState;// listview 当前滚动状态;
boolean isRemark;// 标记,当前是在listview最顶端摁下的;
int state;// 当前的状态;
final int NONE = 0;// 正常状态;
final int PULL = 1;// 提示下拉状态;
final int RELESE = 2;// 提示释放状态;
final int REFLASHING = 3;// 刷新状态;
UpdateData iReflashListener;//刷新数据的接口
//是否启用上拉加载
private boolean isHighPull=true;
private LayoutInflater mInflater;
private LoaDing loaDing;
/**
* 加载前的集合大小
*/
private int count;
/**
* 是否在加载状态
*/
private boolean IsLoadingMore=false;
/**
* 没有更多了
*/
private TextView mLabLoadMore;
/**
* 加载更多
*/
private ProgressBar mProgressBarLoadMore;
// footer view
private RelativeLayout mFooterView;
// footer view
private View header;
//滑动开始坐标
private int startY;
private int firstVisibleItem;
int scrollState;// listview 当前滚动状态;
boolean isRemark;// 标记,当前是在listview最顶端摁下的;
int state;// 当前的状态;
final int NONE = 0;// 正常状态;
final int PULL = 1;// 提示下拉状态;
final int RELESE = 2;// 提示释放状态;
final int REFLASHING = 3;// 刷新状态;
UpdateData iReflashListener;//刷新数据的接口
//是否启用上拉加载
private boolean isHighPull=true;
private LayoutInflater mInflater;
private LoaDing loaDing;
/**
* 加载前的集合大小
*/
private int count;
/**
* 是否在加载状态
*/
private boolean IsLoadingMore=false;
/**
* 没有更多了
*/
private TextView mLabLoadMore;
/**
* 加载更多
*/
private ProgressBar mProgressBarLoadMore;
// footer view
private RelativeLayout mFooterView;
// footer view
private View header;
//滑动开始坐标
private int startY;
private int firstVisibleItem;
public LoadListView(Context context) {
super(context);
init(context);
}
public LoadListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
/**
* 执行加载更多回调
*/
public void setLoaDing(LoaDing loaDing) {
this.loaDing = loaDing;
}
super(context);
init(context);
}
public LoadListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
/**
* 执行加载更多回调
*/
public void setLoaDing(LoaDing loaDing) {
this.loaDing = loaDing;
}
/**
* 初始化
*/
private void init(Context context) {
//添加foot
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// footer
mFooterView = (RelativeLayout) mInflater.inflate(R.layout.load_more_footer, this, false);
mLabLoadMore = (TextView) mFooterView.findViewById(R.id.no_more_textView);
mProgressBarLoadMore = (ProgressBar) mFooterView
.findViewById(R.id.load_more_progressBar);
setFooterDividersEnabled(false);
addFooterView(mFooterView);
super.setOnScrollListener(this);
* 初始化
*/
private void init(Context context) {
//添加foot
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// footer
mFooterView = (RelativeLayout) mInflater.inflate(R.layout.load_more_footer, this, false);
mLabLoadMore = (TextView) mFooterView.findViewById(R.id.no_more_textView);
mProgressBarLoadMore = (ProgressBar) mFooterView
.findViewById(R.id.load_more_progressBar);
setFooterDividersEnabled(false);
addFooterView(mFooterView);
super.setOnScrollListener(this);
//初始化头布局
initView(context);
}
initView(context);
}
public void setiReflashListener(UpdateData iReflashListener) {
this.iReflashListener = iReflashListener;
}
this.iReflashListener = iReflashListener;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.firstVisibleItem=firstVisibleItem;
/**
* 如果当前正在加载中 数据发生了变化就取消加载中进度条 并设置可以继续加载更多
*/
if (IsLoadingMore){
if (count<totalItemCount){
mProgressBarLoadMore.setVisibility(View.GONE);
IsLoadingMore=false;
}
}
if (totalItemCount!=0) {
if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
//监听滚动到底部 进行加载更多操作
if (IsLoadingMore==false&& isHighPull) {
if (loaDing!=null){
//加载的时候保持当前数据数量
count=totalItemCount;
/**
* 如果当前正在加载中 数据发生了变化就取消加载中进度条 并设置可以继续加载更多
*/
if (IsLoadingMore){
if (count<totalItemCount){
mProgressBarLoadMore.setVisibility(View.GONE);
IsLoadingMore=false;
}
}
if (totalItemCount!=0) {
if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
//监听滚动到底部 进行加载更多操作
if (IsLoadingMore==false&& isHighPull) {
if (loaDing!=null){
//加载的时候保持当前数据数量
count=totalItemCount;
IsLoadingMore=true;
//执行加载网络数据方法
loaDing.Load();
LoadMore();
}
}
}
}
}
//加载中
public void LoadMore(){
mProgressBarLoadMore.setVisibility(View.VISIBLE);
mLabLoadMore.setVisibility(View.GONE);
}
//执行加载网络数据方法
loaDing.Load();
LoadMore();
}
}
}
}
}
//加载中
public void LoadMore(){
mProgressBarLoadMore.setVisibility(View.VISIBLE);
mLabLoadMore.setVisibility(View.GONE);
}
/**
* 没有数据时 调用此方法关闭加载更多
*/
public void EndLoadMore(){
mProgressBarLoadMore.post(new Runnable() {
@Override
public void run() {
mProgressBarLoadMore.setVisibility(View.GONE);
}
});
mLabLoadMore.post(new Runnable() {
@Override
public void run() {
mLabLoadMore.setVisibility(View.VISIBLE);
}
});
//关闭加载更多功能
isHighPull=false;
}
* 没有数据时 调用此方法关闭加载更多
*/
public void EndLoadMore(){
mProgressBarLoadMore.post(new Runnable() {
@Override
public void run() {
mProgressBarLoadMore.setVisibility(View.GONE);
}
});
mLabLoadMore.post(new Runnable() {
@Override
public void run() {
mLabLoadMore.setVisibility(View.VISIBLE);
}
});
//关闭加载更多功能
isHighPull=false;
}
/**
* 初始化界面,添加顶部布局文件到 listview
*
* @param context
*/
private void initView(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
header = inflater.inflate(R.layout.load_more_header, null);
measureView(header);
headerHeight = header.getMeasuredHeight();
topPadding(-headerHeight);
this.addHeaderView(header);
this.setOnScrollListener(this);
}
* 初始化界面,添加顶部布局文件到 listview
*
* @param context
*/
private void initView(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
header = inflater.inflate(R.layout.load_more_header, null);
measureView(header);
headerHeight = header.getMeasuredHeight();
topPadding(-headerHeight);
this.addHeaderView(header);
this.setOnScrollListener(this);
}
/**
* 通知父布局,占用的宽,高;
*
* @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;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight,
MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
}
* 通知父布局,占用的宽,高;
*
* @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;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight,
MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
}
/**
* 设置header 布局 上边距;
*
* @param topPadding
*/
private void topPadding(int topPadding) {
header.setPadding(header.getPaddingLeft(), topPadding,
header.getPaddingRight(), header.getPaddingBottom());
header.invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (firstVisibleItem == 0) {
isRemark = true;
startY = (int) ev.getY();
}
break;
* 设置header 布局 上边距;
*
* @param topPadding
*/
private void topPadding(int topPadding) {
header.setPadding(header.getPaddingLeft(), topPadding,
header.getPaddingRight(), header.getPaddingBottom());
header.invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (firstVisibleItem == 0) {
isRemark = true;
startY = (int) ev.getY();
}
break;
case MotionEvent.ACTION_MOVE:
onMove(ev);
break;
case MotionEvent.ACTION_UP:
if (state == RELESE) {
state = REFLASHING;
// 加载最新数据;
reflashViewByState();
if (iReflashListener!=null) {
iReflashListener.update();
}
} else if (state == PULL) {
state = NONE;
isRemark = false;
reflashViewByState();
}
break;
}
return super.onTouchEvent(ev);
}
onMove(ev);
break;
case MotionEvent.ACTION_UP:
if (state == RELESE) {
state = REFLASHING;
// 加载最新数据;
reflashViewByState();
if (iReflashListener!=null) {
iReflashListener.update();
}
} else if (state == PULL) {
state = NONE;
isRemark = 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) {
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;
isRemark = false;
reflashViewByState();
}
break;
}
}
* 判断移动过程操作;
*
* @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) {
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;
isRemark = 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;
* 根据当前状态,改变界面显示;
*/
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;
}
}
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;
}
}
/**
* 获取完数据; //通知listview 刷新数据完毕;
*/
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.setVisibility(VISIBLE);
lastupdatetime.setText(time);
}
//加载更多回调接口
interface LoaDing{
void Load();
}
//刷新回调接口
interface UpdateData{
void update();
}
* 获取完数据; //通知listview 刷新数据完毕;
*/
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.setVisibility(VISIBLE);
lastupdatetime.setText(time);
}
//加载更多回调接口
interface LoaDing{
void Load();
}
//刷新回调接口
interface UpdateData{
void update();
}
}
//调用
//获取上拉加载更多回调
LoadListView .setLoaDing(new LoaDing() {
@Override
public void Load() {
}
});
@Override
public void Load() {
}
});
//获取下拉刷新回掉
LoadListView .setiReflashListener(new UpdateData() {
@Override
public void update() {
@Override
public void update() {
//数据加载完成后 关闭加载中
lv.reflashComplete();
}
});
lv.reflashComplete();
}
});