网上找到些例子,功能都实现的很好,各种刷新,功能多,也就意味着代码多,想定制自己的下拉刷新,无从下手。
自己写了个用于下拉刷新的最简单的例子,简单到只有下拉的界面变化,什么时候刷新,什么时候该处理什么,请自己动手。
此代码就像写程序的“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);
}
}