自定义控件之ListView下拉刷新,上拉加载更多

1、RefleshListView.java

package com.example.refleshlistviewdemo;

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.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class RefleshListView extends ListView {
	private int downY; //记录按下时Y的坐标
	private int moveY; //记录移动时Y的坐标
	private int headerViewHeight; //头部视图的高度
	private View headerView; //头部视图
	private int firstVisiablePosition = 0; //Item中第一个可见的位置
	/**
	 * 头部状态:下拉刷新-0     释放刷新-1        正在刷新-2
	 */
	private int headerStatus = 0;
	private ImageView arrow; //头部视图中的箭头
	private ProgressBar progressBar; //头部视图中的滚动圈
	private TextView statusTextView; //头部视图中的状态文本
	private RotateAnimation downToUpAnim, upToDownAnim; //头部视图中箭头的动画
	private OnHeaderRefleshListener onHeaderRefleshListener; //头部视图加载数据回调接口
	private View footView; //底部加载更多视图
	private boolean isLoading = false; //底部视图是否正在加载标志位
	private long totalPageNum; //列表数据总页数
	private long currentPageNum = 1; //当前的页数
	private onFootRefleshListener onFootRefleshListener; //底部视图加载数据的回调接口

	public RefleshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initHeaderLayout(); //初始化头部加载数据的布局
		initFootLayout(); //初始化底部加载数据的布局
	}

	/**
	 * 初始化头部加载数据的布局
	 */
	private void initHeaderLayout() {
		headerView = LayoutInflater.from(getContext()).inflate(
				R.layout.listview_header_view, null);
		arrow = (ImageView) headerView.findViewById(R.id.arrow);
		progressBar = (ProgressBar) headerView.findViewById(R.id.progreebar);
		statusTextView = (TextView) headerView
				.findViewById(R.id.status_textview);
		//初始化头部视图箭头动画
		initHeaderAnim();
		//测量头视图
		measureView(headerView);
		//获得头视图的高度
		headerViewHeight = headerView.getMeasuredHeight();
		//初始化头部视图为完全隐藏
		headerView.setPadding(0, -headerViewHeight, 0, 0);

		this.addHeaderView(headerView);
		this.setOnScrollListener(new ListViewScrollListener());
	}

	/**
	 * 初始化脚部加载数据布局
	 */
	public void initFootLayout() {
		footView = LayoutInflater.from(getContext()).inflate(
				R.layout.listview_foot_view, null);
		this.addFooterView(footView);
	}

	/**
	 * 移除脚部加载数据布局
	 */
	public void removeMyFootView() {
		this.removeFooterView(footView);
	}

	/**
	 * 初始化头部视图箭头动画
	 */
	public void initHeaderAnim() {
		//从下往上旋转180度
		downToUpAnim = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		downToUpAnim.setFillAfter(true);
		downToUpAnim.setDuration(500);
		//从上往下旋转180度
		upToDownAnim = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		upToDownAnim.setFillAfter(true);
		upToDownAnim.setDuration(500);
	}

	/**
	 * 触摸事件
	 */
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			downY = (int) ev.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			//如果当前的头部加载数据状态为 刷新状态,那么就不能下拉,但是ListView能正常上拉
			if (headerStatus == 2) {
				break;
			}

			moveY = (int) ev.getY();
			int distanceY = (moveY - downY) / 2;// 除以2是为了实际移动的距离和下拉的距离有差距
			if (distanceY > 0 && firstVisiablePosition == 0) {
				int paddingTop = -headerViewHeight + distanceY;
				if (paddingTop < 0 && headerStatus == 1) {
					headerStatus = 0;
					statusTextView.setText("下拉刷新");
					arrow.startAnimation(upToDownAnim);
				} else if (paddingTop > 0 && headerStatus == 0) {
					headerStatus = 1;
					statusTextView.setText("释放刷新");
					arrow.startAnimation(downToUpAnim);
				}
				headerView.setPadding(0, paddingTop, 0, 0);
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:
			//释放时,有两种情况,第一种是头部的状态还是下拉刷新,那么就完全隐藏整个头布局
			//第二种情况是头部的状态是释放刷新,那么就完全显示整个头布局,进入正在刷新的状态
			if (headerStatus == 0) {
				headerView.setPadding(0, -headerViewHeight, 0, 0);
			} else if (headerStatus == 1) {
				headerStatus = 2;
				headerView.setPadding(0, 0, 0, 0);
				arrow.clearAnimation(); // 必须调用,否则arrow.setVisibility属性无效
				arrow.setVisibility(View.GONE);
				progressBar.setVisibility(View.VISIBLE);
				statusTextView.setText("正在刷新...");

				if (onHeaderRefleshListener != null) {
					//回调,这里去开启子线程获取网络数据
					onHeaderRefleshListener.refleshData();
				}
			}
			break;

		default:
			break;
		}
		return super.onTouchEvent(ev);
	}

	/**
	 * ListView滚动监听
	 * 
	 * @author Administrator
	 * 
	 */
	private class ListViewScrollListener implements OnScrollListener {

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			//这个用于记载第一个可见布局的位置,用于头部加载数据中
			firstVisiablePosition = firstVisibleItem;
			//这个用于脚部加载数据的监听
			if ((totalItemCount == firstVisibleItem + visibleItemCount)
					&& !isLoading && currentPageNum < totalPageNum) {
				isLoading = true;
				addFooterView(footView);
				currentPageNum++;
				if(onFootRefleshListener != null){
					onFootRefleshListener.refleshData();
				}
			}
		}

		@Override
		public void onScrollStateChanged(AbsListView arg0, int arg1) {

		}

	}

	/**
	 * 由于还没有执行onMeasure方法,所以只能自己去测试其中一个Item的高度
	 * 
	 * @param child
	 */
	private void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
					MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

	/**
	 * 头部加载完数据,要做一些初始化的处理。
	 */
	public void onHeaderRefleshFinish() {
		// 隐藏头布局
		headerView.setPadding(0, -headerViewHeight, 0, 0);
		// 初始化数据
		headerStatus = 0;
		progressBar.setVisibility(View.GONE);
		arrow.setVisibility(View.VISIBLE);
		statusTextView.setText("下拉刷新");
	}
	
	/**
	 * 脚步加载完数据,要做一些初始化的处理。
	 */
	public void onFootRefleshFinish(){
		isLoading = false;
		removeMyFootView();
	}
	
	/**
	 * 设置数据的总页数
	 * @param totalPageNum
	 */
	public void setTotalPageNum(long totalPageNum){
		this.totalPageNum = totalPageNum;
	}

	/**
	 * 设置头视图监听
	 * @param onHeaderRefleshListener
	 */
	public void setOnHeaderRefleshListener(
			OnHeaderRefleshListener onHeaderRefleshListener) {
		this.onHeaderRefleshListener = onHeaderRefleshListener;
	}

	/**
	 * 设置脚视图的监听
	 * @param onFootRefleshListener
	 */
	public void setOnFootRefleshListener(
			onFootRefleshListener onFootRefleshListener) {
		this.onFootRefleshListener = onFootRefleshListener;
	}

	/**
	 * 刷新完成数据后,必须调用OnHeaderRefleshListener()方法进行隐藏头布局
	 * 
	 * @author Administrator
	 * 
	 */
	public interface OnHeaderRefleshListener {
		public void refleshData();
	}

	/**
	 * 刷新完成数据后,必须调用onFootRefleshFinish()方法进行数据初始化
	 * 
	 * @author Administrator
	 * 
	 */
	public interface onFootRefleshListener {
		public void refleshData();
	}

}

2、MainActivity.java

public class MainActivity extends Activity {
	private RefleshListView refleshListView;
	private List<String> data;
	private ListViewAdapter listViewAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		refleshListView = (RefleshListView) findViewById(R.id.reflesh_listview);
		refleshListView
				.setOnHeaderRefleshListener(new MyHeaderRefleshListener());
		refleshListView.setOnFootRefleshListener(new MyFootRefleshListener());
		// 模拟数据
		data = new ArrayList<String>();
		for (int i = 0; i < 20; i++) {
			data.add("Hello World..." + i);
		}

		listViewAdapter = new ListViewAdapter();
		refleshListView.setAdapter(listViewAdapter);
		refleshListView.removeMyFootView();
		//设置总页数
		refleshListView.setTotalPageNum(3);
	}

	private class MyHeaderRefleshListener implements OnHeaderRefleshListener {

		@Override
		public void refleshData() {
			new Thread() {
				public void run() {
					data.add(0, "这是Header增加的数据...");
					try {
						Thread.sleep(2500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					Message msg = Message.obtain();
					msg.arg1 = 1;
					handler.sendMessage(msg);
				}
			}.start();
		}

	}

	private class MyFootRefleshListener implements onFootRefleshListener {

		@Override
		public void refleshData() {
			new Thread() {
				public void run() {
					try {
						Thread.sleep(2500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					data.add(data.size(), "这是底部Foot新增的数据1");
					data.add(data.size(), "这是底部Foot新增的数据2");
					data.add(data.size(), "这是底部Foot新增的数据3");
					data.add(data.size(), "这是底部Foot新增的数据4");
					data.add(data.size(), "这是底部Foot新增的数据5");

					Message msg = Message.obtain();
					msg.arg1 = 2;
					handler.sendMessage(msg);
				}
			}.start();
		}

	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			listViewAdapter.notifyDataSetChanged();
			if (msg.arg1 == 1) {
				refleshListView.onHeaderRefleshFinish();
			} else if (msg.arg1 == 2) {
				refleshListView.onFootRefleshFinish();
			}
		}
	};

	private class ListViewAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			TextView tv = null;
			if (arg1 == null) {
				tv = new TextView(MainActivity.this);
			} else {
				tv = (TextView) arg1;
			}

			tv.setText(data.get(arg0));
			tv.setPadding(10, 18, 10, 18);
			tv.setTextSize(20);

			return tv;
		}

	}

}

3、listview_header_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:gravity="center">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginTop="25dp"
        android:layout_marginBottom="25dp">

        <ImageView
            android:id="@+id/arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            android:background="@drawable/down_arrow" />

        <ProgressBar
            android:id="@+id/progreebar"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="10dp"
            android:indeterminateDuration="1000"
            android:visibility="gone"
            android:indeterminateDrawable="@anim/progressbar_rotate" />
    </RelativeLayout>
    
    <TextView 
        android:id="@+id/status_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下拉刷新"
        android:layout_marginLeft="10dp"
        android:textColor="#989898"
        android:textSize="18sp"/>

</LinearLayout>

4、listview_foot_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" 
    android:layout_marginTop="10dp"
    android:paddingBottom="10dp">

    <ProgressBar
        android:id="@+id/progreebar"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="10dp"
        android:indeterminateDrawable="@anim/progressbar_rotate"
        android:indeterminateDuration="1000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="加载更多..."
        android:textColor="#989898"
        android:textSize="18sp" />

</LinearLayout>

5、main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.refleshlistviewdemo.RefleshListView
        android:id="@+id/reflesh_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

6、progress_bar_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/progress_bars_1"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="-1"
    android:duration="500"
    android:toDegrees="360" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值