package
com.wang.activity;
import
android.app.Activity;
import
android.os.Bundle;
import
android.os.SystemClock;
import
android.util.Log;
import
android.widget.ListView;
import
com.wang.bean.MyDate;
import
com.wang.util.MyBaseAdapter;
import
com.wang.util.MyListView;
import
com.wang.util.MyTast;
import
java.util.ArrayList;
import
java.util.List;
public
class
MainActivity
extends
Activity {
private
MyListView myListView;
private
MyBaseAdapter myBaseAdapter;
private
ArrayList<MyDate.ListBean> list=
new
ArrayList<MyDate.ListBean>();
//对于联网数据,传的是空数据
private
Boolean isFirst=
true
;
private
Boolean isstaticBl=
false
;
//用于存储从网站读取的数据
private
ArrayList<String> listTitle =
new
ArrayList<String>();
private
ArrayList<String> listImage =
new
ArrayList<String>();
private
ArrayList<String> listDate =
new
ArrayList<String>();
private
int
pageNum=
3
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (MyListView) findViewById(R.id.at_lv);
myBaseAdapter=
new
MyBaseAdapter(list,getLayoutInflater());
getInfoTast();
myListView.setAdapter(myBaseAdapter);
myListView.setDataCallBack(
new
MyListView.DataCallBack() {
@Override
public
void
footCallBack() {
isstaticBl=
false
;
getInfoTast();
}
@Override
public
void
headCallBack() {
isstaticBl=
true
;
getInfoTast();
}
});
}
private
void
getInfoTast() {
if
(pageNum>=
7
){
if
(isstaticBl){
myListView.headViewGone();
}
else
{
myListView.footViewNoInfo();
}
}
else
{
new
MyTast<MyDate>(MyDate.
class
,
new
MyTast.DateCallBack<MyDate>(
) {
@Override
public
void
Callback(MyDate myDate) {
List<MyDate.ListBean> tempList = myDate.getList();
list.addAll(tempList);
myBaseAdapter.notifyDataSetChanged();
//为了第一次完美,不让else运行,其实运行也无所谓
if
(isstaticBl){
myListView.headViewGone();
}
else
if
(!isFirst){
myListView.footViewGone();
}
if
(isFirst){
myListView.headViewGone();
isFirst=
false
;
}
}
}).execute(
"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=0&page="
+pageNum+
"&vs=and412&retina=1"
);
pageNum++;
}
}
}
package
com.wang.util;
import
android.animation.ObjectAnimator;
import
android.content.Context;
import
android.util.AttributeSet;
import
android.util.Log;
import
android.view.View;
import
android.widget.AbsListView;
import
android.widget.ImageView;
import
android.widget.ListAdapter;
import
android.widget.ListView;
import
android.widget.ProgressBar;
import
android.widget.TextView;
import
com.wang.activity.R;
import
java.util.Queue;
/**
* Created by Administrator on 2016/4/10.
* 单参构造方法
*/
public
class
MyListView
extends
ListView
implements
AbsListView.OnScrollListener {
private
View headView, footView;
private
ProgressBar mylistview_footview_pb, mylistview_headview_pb;
private
TextView mylistview_footview_tv, mylistview_headview_tv;
private
ImageView mylistview_headview_iv;
private
ObjectAnimator rotateani;
//判断是否滑到了底部,默认为false
private
Boolean isLandbl =
false
;
//上拉刷新中,判断,在符合的滑动中,动画执行一次,默认为false;
private
Boolean isFirst =
false
;
//判断是不是最后一次,此值,由mainActivity赋值
private
Boolean isLast=
false
;
//为借口回调声明变量,以便于赋值Activity(识别调用哪个activity)
private
DataCallBack dataCallBack;
//控制不让listView缩回去
private
int
firstitem;
/**
* 接口回调,为了接收新数据
*
* @author WangShujie
* created at 2016/4/10 20:08
*/
public
interface
DataCallBack {
void
footCallBack();
void
headCallBack();
}
/**
* 为接口回调赋值
*
* @author WangShujie
* created at 2016/4/10 20:17
*/
public
void
setDataCallBack(DataCallBack dataCallBack) {
this
.dataCallBack = dataCallBack;
}
/**
* 单参构造方法,new 类的时候调用
*
* @author WangShujie
* created at 2016/4/10 10:26
*/
public
MyListView(Context context) {
super
(context);
init(context);
}
/**
* 双参的构造方法,引用布局的时候调用
*
* @author WangShujie
* created at 2016/4/10 10:27
*/
public
MyListView(Context context, AttributeSet attrs) {
super
(context, attrs);
init(context);
}
/**
* 初始化操作,放在构造中显得代码太多,冗杂,单独抽取出来了
*
* @author WangShujie
* created at 2016/4/10 10:28
*/
private
void
init(Context context) {
//下拉刷新控件系列的寻找
footView = View.inflate(context, R.layout.mylistview_footview,
null
);
mylistview_footview_pb = (ProgressBar) footView.findViewById(R.id.mylistview_footview_pb);
mylistview_footview_tv = (TextView) footView.findViewById(R.id.mylistview_footview_tv);
//用户第一次进入界面后,对下拉刷新控件系列的显示设置
addFooterView(footView);
//上拉加载控件系列的寻找
headView = View.inflate(context, R.layout.mylistview_headview,
null
);
mylistview_headview_pb = (ProgressBar) headView.findViewById(R.id.mylistview_headview_pb);
mylistview_headview_tv = (TextView) headView.findViewById(R.id.mylistview_headview_tv);
mylistview_headview_iv = (ImageView) headView.findViewById(R.id.mylistview_headview_ig);
addHeaderView(headView);
//没有接受到数据前,headView的显示效果设定
getHeadAni(R.mipmap.headview_pre);
mylistview_headview_tv.setText(
"请稍候,数据正在加载中。。。"
);
footViewGone();
// 对滚动进行监听
setOnScrollListener(
this
);
}
/**
* 考虑到刷新时,图片需要变化,封装图片旋转的方法
*
* @author WangShujie
* created at 2016/4/10 20:39
*/
private
void
getHeadAni(
int
resource) {
mylistview_headview_iv.setImageResource(resource);
rotateani = ObjectAnimator.ofFloat(mylistview_headview_iv,
"rotation"
,
0
,
360
);
rotateani.setDuration(
2000
);
rotateani.setRepeatCount(-
1
);
}
/**
* 当滑动状态改变的时候
*
* @author WangShujie
* created at 2016/4/10 19:58
*/
@Override
public
void
onScrollStateChanged(AbsListView view,
int
scrollState) {
if
(scrollState == SCROLL_STATE_IDLE && isLandbl) {
//松手后显示正在加载
// footViewVisible();
dataCallBack.footCallBack();
}
else
if
(scrollState == SCROLL_STATE_IDLE && firstitem ==
0
) {
if
(!isFirst) {
getHeadAni(R.mipmap.headview_last);
mylistview_headview_tv.setText(
"正在刷新。。。"
);
dataCallBack.headCallBack();
}
else
{
setSelectionFromTop(
1
,
0
);
}
}
}
/**
* 当滑动的时候
*
* @author WangShujie
* created at 2016/4/10 19:58
*/
@Override
public
void
onScroll(AbsListView view,
int
firstVisibleItem,
int
visibleItemCount,
int
totalItemCount) {
firstitem = firstVisibleItem;
//totalItemCount>2是为了第一次显示的时候不让 footView显示(footView 显示方式2才用)
if
(firstVisibleItem + visibleItemCount == totalItemCount&&totalItemCount>
2
) {
if
(!isLast){
footViewVisible();
}
isLandbl =
true
;
}
else
{
isLandbl =
false
;
}
if
(firstVisibleItem <=
1
&& headView.getHeight() !=
0
) {
if
(headView.getBottom() >= headView.getHeight() *
2
/
3
) {
// /*
// * 第一次进入程序,会运行到这,因为headView.getBottom()默认的是==headView.getHeight()
// * (其实,刚开始进入程序,headView会显示,也就是那时读取的高度,所以==headView.getHeight()
// * 当拉动headview的时候,每次滑动都会读取到getBottom,此时撒手,headView最终弹回,向下拉时,
// * 还是读取到的getBottom,比如拉到getBottom(100,随便取得)>getHeight,会显示正在刷新,然后向下拉
// * getBottom还是100)
// * 而且firstVisibleItem=1,以上两个if语句都成立,只是下面这个语句不成立()
// *
// * */
if
(isFirst) {
mylistview_headview_tv.setText(
"松手刷新"
);
getHeadAni(R.mipmap.headview_mid);
rotateani.start();
isFirst =
false
;
}
}
else
{
if
(!isFirst) {
mylistview_headview_tv.setText(
"上拉刷新"
);
getHeadAni(R.mipmap.headview_pre);
rotateani.start();
isFirst =
true
;
}
}
}
}
/**
* footView显示出来
*
* @author WangShujie
* created at 2016/4/10 20:04
*/
public
void
footViewVisible() {
mylistview_footview_pb.setVisibility(VISIBLE);
mylistview_footview_tv.setVisibility(VISIBLE);
}
/**
* footView隐藏起来
*
* @author WangShujie
* created at 2016/4/10 21:36
*/
public
void
footViewGone() {
mylistview_footview_pb.setVisibility(GONE);
mylistview_footview_tv.setVisibility(GONE);
}
/**
* 没有更多数据的时候,一个隐藏一个显示
*
* @author WangShujie
* created at 2016/4/11 8:43
*/
public
void
footViewNoInfo() {
isLast=
true
;
mylistview_footview_pb.setVisibility(GONE);
mylistview_footview_tv.setText(
"没有更多数据了!!!"
);
mylistview_footview_tv.setVisibility(VISIBLE);
}
/**
* headView显示
*
* @author WangShujie
* created at 2016/4/11 8:44
*/
public
void
headViewVisible() {
mylistview_headview_tv.setVisibility(VISIBLE);
mylistview_headview_iv.setVisibility(VISIBLE);
}
/**
* headView隐藏
*
* @author WangShujie
* created at 2016/4/11 8:45
*/
public
void
headViewGone() {
setSelectionFromTop(
1
,
0
);
}
public
void
setHeadView() {
addHeaderView(headView);
setSelectionFromTop(
1
,
0
);
}
}