正文  软件开发 > 编程综合 >

Android自定义下拉刷新

网上的下拉刷新功能很多,不过基本上都是隐藏header的,而项目里面需要只隐藏部分的header,类似QQ好友动态的效果,修改了一些现有的,最后有很多问题,所以就自己自定义了一个,逻辑也很简单...

网上的下拉刷新功能很多,不过基本上都是隐藏header的,而项目里面需要只隐藏部分的header,类似QQ好友动态的效果,修改了一些现有的,最后有很多问题,所以就自己自定义了一个,逻辑也很简单,首先就是重写ListView,然后覆写onTouchEvent,然后根据手的触摸位置计算差值,然后移动header.下面是效果图:

\

\

\

主要的部分就是下拉刷新组件,代码如下,里面我都要注释:

package com.jwzhangjie.mypullrefresh;

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.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PullReFreshLibrary extends ListView{
	//下拉的父布局
	private LinearLayout conterViewLayout;
	//实现下拉的头部
	private RelativeLayout headerView;
	//提示下拉状态
	private TextView pullTextView;
	private LayoutInflater mInflater;
	//下拉框的高度
	private int headviewHeight;
	//下拉框距离顶部的初始高度
	private int initHeight;
	//手按下去的y轴坐标
	private int initPosition = 0;
	//最新的手的y轴坐标
	private int lastPositon =0;
	//下拉移动的最小距离
	private int minMarge = 5;
	//当前下拉head的距离顶部的高度
	private int currentHeight = -100;
	//下拉head的各种状态
	private static enum State{
        PULL_TO_REFRESH,
        RELEASE_TO_REFRESH,
        REFRESHING,
        REFRESHCOMPLETE,
    }
	private State state = State.PULL_TO_REFRESH;
	
	public PullReFreshLibrary(Context context, AttributeSet attrs) {
		super(context, attrs);
		mInflater = LayoutInflater.from(context);
		conterViewLayout = (LinearLayout) mInflater.inflate(R.layout.headview, null);
		headerView = (RelativeLayout)conterViewLayout.findViewById(R.id.testHeader);
		pullTextView = (TextView)headerView.findViewById(R.id.pullTextView);
		headviewHeight = (int)context.getResources().getDimensionPixelSize(R.dimen.header_view_height);
		initHeight = (int)context.getResources().getDimensionPixelSize(R.dimen.init_height);
		addHeaderView(conterViewLayout);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (state == State.REFRESHING) {
			return true;
		}
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			initPosition = (int)ev.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			if (getFirstVisiblePosition() == 0) {
				lastPositon = (int)ev.getY();
				int diff = lastPositon - initPosition;
				if (Math.abs(diff) > minMarge) {
					diff = (int)(diff / 2.7);
				}
				initPosition = lastPositon;
				int newHeaderPadding = Math.max(Math.round(currentHeight + diff),  -headviewHeight);
				if (state != State.REFRESHING && newHeaderPadding != currentHeight) {
					if (newHeaderPadding > 0) {
						currentHeight = 0;
					}else {
						currentHeight = newHeaderPadding;
					}
					margeTop();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			if (state == State.RELEASE_TO_REFRESH) {
				setState(State.RELEASE_TO_REFRESH);
			}else if (getFirstVisiblePosition() == 0 && currentHeight > -initHeight) {
				resetHeader();
			}
			break;
		}
		return super.onTouchEvent(ev);
	}

	/**
	 * 设置下拉head的状态以及处理功能
	 * @param state
	 */
	private void setState(State state){
		this.state = state;
		switch (state) {
		case PULL_TO_REFRESH:
			pullTextView.setVisibility(View.VISIBLE);
			pullTextView.setText("下拉刷新");
			break;
		case RELEASE_TO_REFRESH:
			if (onRefreshListener == null) {
				state = State.PULL_TO_REFRESH;
				resetHeader();
			}else {
				state = State.REFRESHING;
				pullTextView.setVisibility(View.VISIBLE);
				pullTextView.setText("刷新中...");
				onRefreshListener.onRefresh();
			}
			break;
		case REFRESHCOMPLETE:
			state = State.PULL_TO_REFRESH;
			pullTextView.setText("下拉刷新");
			resetHeader();
			break;
		}
	}
	/**
	 * 设置下拉head距离顶部的高度
	 */
	private void margeTop(){
		MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) headerView.getLayoutParams();
		mlp.topMargin = currentHeight;
        headerView.setLayoutParams(mlp);
        if (currentHeight > -10 && state != State.RELEASE_TO_REFRESH) {
        	state = State.RELEASE_TO_REFRESH;
        	pullTextView.setText("松手刷新");
		}else if (currentHeight 


适配器adater,这个很简陋,主要是辅助测试

package com.jwzhangjie.mypullrefresh;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{

	private LayoutInflater mInflater;
	public MyAdapter(Context context){
		mInflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		return 19;
	}

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

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView textView;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_listview, null);
			textView = (TextView)convertView.findViewById(R.id.item_test);
			convertView.setTag(textView);
		}else {
			textView = (TextView)convertView.getTag();
		}
		textView.setText("测试数据:"+position);
		return convertView;
	}
	
}


然后就是Activity的调用:

package com.jwzhangjie.mypullrefresh;

import com.jwzhangjie.mypullrefresh.PullReFreshLibrary.OnRefreshListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity {

	private MyAdapter myAdapter;
	private PullReFreshLibrary listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myAdapter = new MyAdapter(this);
		listView = (PullReFreshLibrary)findViewById(R.id.testpull);
		listView.setAdapter(myAdapter);
		listView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				handler.sendEmptyMessageDelayed(1, 4000);
			}
		});
	}
	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			listView.onRefreshComplete();
		}
		
	};
}


我里面的布局也都很简单,

activity_main.xml

.jwzhangjie.mypullrefresh.PullReFreshLibrary>


headview.xml


item_listview.xml


 48dip300dip