GridView翻页和listview类似,但是他没用addHeaderView和addFooterView方法,这个需要自己在界面上加,另外需要解决事件冲突,滚动到底部计算等问题,尝试了很多次,最终整出来一个能用的了。
package com.sharera.capitalcircle.view;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import com.sharera.capitalcircle.R;
import com.sharera.capitalcircle.activity.VideoPlayActivity;
import com.sharera.capitalcircle.adapter.NewsVideoAdapter;
/**
* 上拉刷新,下拉加载GridView
*
* @author Administrator
*
*/
public class MyGridView extends GridView implements OnScrollListener,OnTouchListener {
private final static int RELEASE_To_REFRESH = 0; // 下拉完成
private final static int PULL_To_REFRESH = 1; // 下拉中
private final static int REFRESHING = 2; // 正在刷新列表
private final static int DONE = 3; // 初始状态
private final static int LOADING = 4; //
private final static int RATIO = 3;
private LayoutInflater inflater;
private LinearLayout headView;
private LinearLayout footerView;
private TextView tipsTextview;
private TextView lastUpdatedTextView;
private ImageView arrowImageView;
private ProgressBar progressBar;
private ProgressBar footer_progressBar;
private TextView load_more_data;
private RotateAnimation animation;
private RotateAnimation reverseAnimation;
private boolean isRecored;
private int headContentWidth; // 头部内容实际宽度
private int headContentHeight; // 头部内容实际高度
private int footerContentWidth;
private int footerContentHeight;
private int startY;
public int firstItemIndex;
private int totalItemCount;
private int footerLastItem;
private int state;
private boolean isBack;
private OnRefreshListener refreshListener;
private boolean isLoading;
public boolean hasMore = true; // 是否有更多记录
private int pageIndex; // 当前页码
private int index=0;
private int getLastVisiblePosition = 0, lastVisiblePositionY = 0;
public boolean firstDownPullFooter = false;
//public ScrollView scroll;
public MyGridView(Context context) {
super(context);
init(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public View getHeadView() {
return headView;
}
public View getFooterView() {
return footerView;
}
private void init(Context context) {
// setCacheColorHint(context.getResources().getColor(R.color.list_view_cache_color));
inflater = LayoutInflater.from(context);
// 列表头部
headView = (LinearLayout) inflater.inflate(R.layout.mylistview_head,
null);
arrowImageView = (ImageView) headView
.findViewById(R.id.head_arrowImageView);
progressBar = (ProgressBar) headView
.findViewById(R.id.head_progressBar);
tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
lastUpdatedTextView = (TextView) headView
.findViewById(R.id.head_lastUpdatedTextView);
measureView(headView);
headContentHeight = headView.getMeasuredHeight();
headContentWidth = headView.getMeasuredWidth();
// 初始显示时,隐藏头部
headView.setPadding(0, -1 * headContentHeight, 0, 0);
headView.invalidate();
// 列表尾部
footerView = (LinearLayout) inflater.inflate(
R.layout.mylistview_footer, null);
footer_progressBar = (ProgressBar) footerView
.findViewById(R.id.load_data_progress);
load_more_data = (TextView) footerView
.findViewById(R.id.load_more_data);
load_more_data.setText("加载更多");
measureView(footerView);
footerContentHeight = footerView.getMeasuredHeight();
footerContentWidth = footerView.getMeasuredWidth();
// 初始显示时,隐藏尾部
footerView.setPadding(0, -1 * footerContentHeight, 0, 0);
footerView.invalidate();
this.setOnScrollListener(this);
// 设置动画
animation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF