Android ListView下拉刷新,最基本代码

网上找到些例子,功能都实现的很好,各种刷新,功能多,也就意味着代码多,想定制自己的下拉刷新,无从下手。

自己写了个用于下拉刷新的最简单的例子,简单到只有下拉的界面变化,什么时候刷新,什么时候该处理什么,请自己动手。

此代码就像写程序的“Hello Word”,带你入门,弄懂基本原理,高深的部分,网上已经有人做的很好了,推荐个各种刷新的源码:android-pulltorefresh-master

注:以下代码不需要layout文件,由代码布局,由代码生成ListView所需数据,尽量简化,希望能帮到读者。

package com.kevinshq.pulltorefresh;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PullToRefreshActivity extends Activity {
	public static final String TAG = "PullToRefreshActivity";

	private final static int RATIO = 2;
	private boolean isRecored;

	private int firstItemIndex = 0;
	private float startY = 0;

	private TextView headTextView = null;
	private ListView listView = null;

	private View.OnTouchListener onTouchListener = new View.OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			float tempY = event.getY();
			firstItemIndex = listView.getFirstVisiblePosition();
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
			case MotionEvent.ACTION_MOVE:
				if (!isRecored && (firstItemIndex == 0)) {
					isRecored = true;
					startY = tempY;
				}
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				isRecored = false;
				break;

			default:
				break;
			}

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				if (tempY > startY) {
					listView.setPadding(0, -200, 0, 0);
				}
				break;
			case MotionEvent.ACTION_MOVE:
				if (isRecored && tempY > startY) {
					listView.setPadding(0, (int) ((tempY-startY)/RATIO-200), 0, 0);
				}
				break;

			default:
				break;
			}
			return false;
		}
	};

	private ListAdapter adapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv = (TextView) convertView;
			if (tv == null) {
				tv = new TextView(getApplicationContext());
			}
			tv.setGravity(Gravity.CENTER);
			tv.setMinHeight(50);
			tv.setText(String.valueOf(position));
			tv.setBackgroundColor(Color.GRAY);
			return tv;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 50;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		ViewGroup.LayoutParams lp_ViewGroup = new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		LinearLayout linearLayout = new LinearLayout(this);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		setContentView(linearLayout, lp_ViewGroup);

		headTextView = new TextView(this);
		headTextView.setGravity(Gravity.CENTER);
		headTextView.setMinHeight(200);
		headTextView.setText("Head");
		headTextView.setTypeface(Typeface.DEFAULT_BOLD);
		headTextView.setTextSize(50);
		headTextView.setBackgroundColor(Color.GREEN);
		headTextView.invalidate();

		LinearLayout.LayoutParams lp_LinearLayout = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		listView = new ListView(this);
		listView.addHeaderView(headTextView, null, false);
		listView.setOnTouchListener(onTouchListener);
		listView.setAdapter(adapter);
		listView.setPadding(0, -200, 0, 0);
		linearLayout.addView(listView, lp_LinearLayout);
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值