GridView翻页

本文介绍了在Android中处理GridView翻页的问题,包括如何在界面中添加头部和尾部,解决滚动事件冲突以及如何判断滚动到底部进行加载更多数据的操作。
摘要由CSDN通过智能技术生成

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值