代码结构,主要是两个类,实现头部功能的封装,写出一个自定义控件
1、头部封装类
/**
* 该类主要是完成 头部部分的功能封装
*
* 一个可以监听ListView是否滚动到最顶部或最底部的自定义控件 只能监听由触摸产生的,如果是ListView本身Flying导致的,则不能监听</br>
* 如果加以改进,可以实现监听scroll滚动的具体位置等
*
* @author 进
*/
public class ScrollOverListView extends ListView implements OnScrollListener {
private int mLastY;
private int mBottomPosition;
private static final String TAG = "listview";
/** 松开更新 **/
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;
/** 实际的padding的距离与界面上偏移距离的比例 **/
private final static int RATIO = 3;
private LayoutInflater inflater;
/** 头部刷新的布局 **/
private LinearLayout headView;
/** 头部显示下拉刷新等的控件 **/
private TextView tipsTextview;
/** 刷新控件 **/
private TextView lastUpdatedTextView;
/** 箭头图标 **/
private ImageView arrowImageView;
/** 头部滚动条 **/
private ProgressBar progressBar;
/** 显示动画 **/
private RotateAnimation animation;
/** 头部回退显示动画 **/
private RotateAnimation reverseAnimation;
/** 用于保证startY的值在一个完整的touch事件中只被记录一次 **/
private boolean isRecored;
/** 头部高度 **/
private int headContentHeight;
/** 开始的Y坐标 **/
private int startY;
/** 第一个item **/
private int firstItemIndex;
/** 状态 **/
private int state;
private boolean isBack;
/** 是否要使用下拉刷新功能 **/
public boolean showRefresh = true;
public static boolean canRefleash = true;
public ScrollOverListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public ScrollOverListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ScrollOverListView(Context context) {
super(context);
init(context);
}
/** 出事化控件 **/
private void init(Context context) {
mBottomPosition = 0;
setCacheColorHint(0);
inflater = LayoutInflater.from(context);
headView = (LinearLayout) inflater.inflate(R.layout.pull_down_head,
null);
arrowImageView = (ImageView) headView
.findViewById(R.id.head_arrowImageView);
arrowImageView.setMinimumWidth(70);
arrowImageView.setMinimumHeight(50);
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();
headView.setPadding(0, -1 * headContentHeight, 0, 0);
headView.invalidate();
/** 列表添加头部 **/
addHeaderView(headView, null, false);
setOnScrollListener(this);
animation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(250);
animation.setFillAfter(true);
reverseAnimation = new RotateAnimation(-180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
reverseAnimation.setInterpolator(new LinearInterpolator());
reverseAnimation.setDuration(200);
reverseAnimation.setFillAfter(true);
state = DONE;
}
/** 触摸事件的处理 **/
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
final int y = (int) ev.getRawY();
cancelLongPress();
switch (action) {
case MotionEvent.ACTION_DOWN: { // 按下的时候
if (firstItemIndex == 0 && !isRecored) {
isRecored = true;
startY = (int) ev.getY();
}
mLastY = y;
final boolean isHandled = mOnScrollOverListener.onMotionDown(ev);
if (isHandled) {
mLastY = y;
return isHandled;
}
break;
}
case MotionEvent.ACTION_MOVE: { // 手指正在移动的时候
int tempY = (int) ev.getY();
if (showRefresh) {
if (!isRecored && firstItemIndex == 0) {
Log.v(TAG, "在move时候记录下位置");
isRecored = true;
startY = tempY;
}
if (state != REFRESHING && isRecored && state != LOADING) {
// 保证在设置padding的过程中,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动
// 可以松手去刷新了
if (state == RELEASE_To_REFRESH) {
setSelection(0);
// 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步
if (((tempY - startY) / RATIO < headContentHeight)
&& (tempY - startY) > 0) {
state = PULL_To_REFRESH;
changeHeaderViewByState();
}
// 一下子推到顶了
else if (tempY - startY <= 0) {
state = DONE;
changeHeaderViewByState();
}
// 往下拉了,或者还没有上推到屏幕顶部掩盖head的地步
else {
// 不用进行特别的操作,只用更新paddingTop的值就行了
}
}
// 还没有到达显示松开刷新的时候,DONE或者是PULL_To_REFRESH状态
if (state == PULL_To_REFRESH) {
setSelection(0);
// 下拉到可以进入RELEASE_TO_REFRESH的状态
if ((tempY - startY) / RATIO >= headContentHeight) {
state = RELEASE_To_REFRESH;
isBack = true;
changeHeaderViewByState();
}
// 上推到顶了
else if (tempY - startY <= 0) {
state = DONE;
changeHeaderViewByState();
}
}
// done状态下
if (state == DONE) {
if (tempY - startY > 0) {
state = PULL_To_REFRESH;
changeHeaderViewByState();
}
}
// 更新headView的size
if (state == PULL_To_REFRESH) {
headView.setPadding(0, -1 * headContentHeight
+ (tempY - startY) / RATIO, 0, 0);
}
// 更新headView的paddingTop
if (state == RELEASE_To_REFRESH) {
headView.setPadding(0, (tempY - startY) / RATIO
- headContentHeight, 0, 0);
}
}
}
final int childCount = getChildCount();
if (childCount == 0)
return super.onTouchEvent(ev);
final int itemCount = getAdapter().getCount() - mBottomPosition;
final int deltaY = y - mLastY;
final int lastBottom = getChildAt(childCount - 1).getBottom();
final int end = getHeight() - getPaddingBottom();
final int firstVisiblePosition = getFirstVisiblePosition();
final boolean isHandleMotionMove = mOnScrollOverListener
.onMotionMove(ev, deltaY);
if (isHandleMotionMove) {
mLastY = y;
return true;
}
/** 到达底部 * 到达底部的事件在另外一个类执行 **/
if (firstVisiblePosition + childCount >= itemCount
&& lastBottom <= end && deltaY < 0) {
final boolean isHandleOnListViewBottomAndPullDown;
isHandleOnListViewBottomAndPullDown = mOnScrollOverListener
.onListViewBottomAndPullUp(deltaY);
if (isHandleOnListViewBottomAndPullDown) {
mLastY = y;
return true;
}
}
break;
}
case MotionEvent.ACTION_UP: { // 手指抬起来的时候
if (state != REFRESHING && state != LOADING) {
if (state == DONE) {
// 什么都不做
}
if (state == PULL_To_REFRESH) {
state = DONE;
changeHeaderViewByState();
}
if (state == RELEASE_To_REFRESH) {
state = REFRESHING;
changeHeaderViewByState();
canRefleash = true;
}
}
isRecored = false;
isBack = false;
// /======================
final boolean isHandlerMotionUp = mOnScrollOverListener
.onMotionUp(ev);
if (isHandlerMotionUp) {
mLastY = y;
return true;
}
break;
}
}
mLastY = y;
return super.onTouchEvent(ev);
}
/** 空的 */
private OnScrollOverListener mOnScrollOverListener = new OnScrollOverListener() {
@Override
public boolean onListViewTopAndPullDown(int delta) {
return false;
}
@Override
public boolean onListViewBottomAndPullUp(int delta) {
return false;
}
@Override
public boolean onMotionDown(MotionEvent ev) {
return false;
}
@Override
public boolean onMotionMove(MotionEvent ev, int delta) {
return false;
}
@Override
public boolean onMotionUp(MotionEvent ev) {
return false;
}
};
// =============================== public method
/**
* 可以自定义其中一个条目为头部,头部触发的事件将以这个为准,默认为第一个
*
* @param index
* 正数第几个,必须在条目数范围之内
*/
public void setTopPosition(int index) {
if (getAdapter() == null)
throw new NullPointerException(
"You must set adapter before setTopPosition!");
if (index < 0)
throw new IllegalArgumentException("Top position must > 0");
}
/**
* 可以自定义其中一个条目为尾部,尾部触发的事件将以这个为准,默认为最后一个
*
* @param index
* 倒数第几个,必须在条目数范围之内
*/
public void setBottomPosition(int index) {
if (getAdapter() == null)
throw new NullPointerException(
"You must set adapter before setBottonPosition!");
if (index < 0)
throw new IllegalArgumentException("Bottom position must > 0");
mBottomPosition = index;
}
/**
* 设置这个Listener可以监听是否到达顶端,或者是否到达低端等事件</br>
*
* @see OnScrollOverListener
*/
public void setOnScrollOverListener(
OnScrollOverListener onScrollOverListener) {
mOnScrollOverListener = onScrollOverListener;
}
/**
* 滚动监听接口
*
* @see ScrollOverListView#setOnScrollOverListener(OnScrollOverListener)
*
*/
public interface OnScrollOverListener {
/**
* 到达最顶部触发
*
* @param delta
* 手指点击移动产生的偏移量
* @return
*/
boolean onListViewTopAndPullDown(int delta);
/**
* 到达最底部触发
*
* @param delta
* 手指点击移动产生的偏移量
* @return
*/
boolean onListViewBottomAndPullUp(int delta);
/**
* 手指触摸按下触发,相当于{@link MotionEvent#ACTION_DOWN}
*
* @return 返回true表示自己处理
* @see View#onTouchEvent(MotionEvent)
*/
boolean onMotionDown(MotionEvent ev);
/**
* 手指触摸移动触发,相当于{@link MotionEvent#ACTION_MOVE}
*
* @return 返回true表示自己处理
* @see View#onTouchEvent(MotionEvent)
*/
boolean onMotionMove(MotionEvent ev, int delta);
/**
* 手指触摸后提起触发,相当于{@link MotionEvent#ACTION_UP}
*
* @return 返回true表示自己处理
* @see View#onTouchEvent(MotionEvent)
*/
boolean onMotionUp(MotionEvent ev);
}
@Override
public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,
int arg3) {
firstItemIndex = firstVisiableItem;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
// 此方法直接照搬自网络上的一个下拉刷新的demo,此处是“估计”headView的width以及height
private void measureView(View child) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_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 onRefreshComplete() {
state = DONE;
// 头部更新时候的操作,显示当前系统进行更新的时间
lastUpdatedTextView.setText("最近更新:" + new Date().toLocaleString());
changeHeaderViewByState();
}
// 当状态改变时候,调用该方法,以更新界面
private void changeHeaderViewByState() {
switch (state) {
case RELEASE_To_REFRESH:
arrowImageView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
tipsTextview.setVisibility(View.VISIBLE);
lastUpdatedTextView.setVisibility(View.VISIBLE);
arrowImageView.clearAnimation();
arrowImageView.startAnimation(animation);
tipsTextview.setText("松开刷新");
break;
case PULL_To_REFRESH:
progressBar.setVisibility(View.GONE);
tipsTextview.setVisibility(View.VISIBLE);
lastUpdatedTextView.setVisibility(View.VISIBLE);
arrowImageView.clearAnimation();
arrowImageView.setVisibility(View.VISIBLE);
// 是由RELEASE_To_REFRESH状态转变来的
if (isBack) {
isBack = false;
arrowImageView.clearAnimation();
arrowImageView.startAnimation(reverseAnimation);
tipsTextview.setText("下拉刷新");
} else {
tipsTextview.setText("下拉刷新");
}
break;
case REFRESHING:
headView.setPadding(0, 0, 0, 0);
progressBar.setVisibility(View.VISIBLE);
arrowImageView.clearAnimation();
arrowImageView.setVisibility(View.GONE);
tipsTextview.setText("正在刷新...");
lastUpdatedTextView.setVisibility(View.VISIBLE);
break;
case DONE:
headView.setPadding(0, -1 * headContentHeight, 0, 0);
progressBar.setVisibility(View.GONE);
arrowImageView.clearAnimation();
arrowImageView.setImageResource(R.drawable.pull_down_arrow);
tipsTextview.setText("下拉刷新");
lastUpdatedTextView.setVisibility(View.VISIBLE);
break;
}
}
}
2、要写个自定义控件把这个给加载进去
/**
* 下拉刷新控件 真正实现下拉刷新的是这个控件, ScrollOverListView只是提供触摸的事件等
*
* @author
*/
public class PullDownView extends LinearLayout implements OnScrollOverListener {
private static final int START_PULL_DEVIATION = 50; /** 移动误差*/
private static final int WHAT_DID_MORE = 5; /** Handler what 已经获取完更多*/
private static final int WHAT_DID_REFRESH = 3; /** Handler what 已经刷新完*/
/**底部更多的按键**/
private RelativeLayout mFooterView;
/**底部更多的按键**/
private TextView mFooterTextView;
/**底部更多的按键**/
private ProgressBar mFooterLoadingView;
/**已经含有 下拉刷新功能的列表**/
private ScrollOverListView mListView;
/**刷新和更多的事件接口**/
private OnPullDownListener mOnPullDownListener;
private float mMotionDownLastY; /** 按下时候的Y轴坐标*/
private boolean mIsFetchMoreing; /** 是否获取更多中*/
private boolean mIsPullUpDone; /** 是否回推完成*/
private boolean mEnableAutoFetchMore; /** 是否允许自动获取更多*/
public PullDownView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderViewAndFooterViewAndListView(context);
}
public PullDownView(Context context) {
super(context);
initHeaderViewAndFooterViewAndListView(context);
}
/*
* ================================== Public method 外部使用,具体就是用这几个就可以了
*/
/**
* 刷新和获取更多事件接口
*/
public interface OnPullDownListener {
/**刷新事件接口 这里要注意的是获取更多完 要关闭 刷新的进度条RefreshComplete()**/
void onRefresh();
/**刷新事件接口 这里要注意的是获取更多完 要关闭 更多的进度条 notifyDidMore()**/
void onMore();
}
/**
* 通知已经获取完更多了,要放在Adapter.notifyDataSetChanged后面
* 当你执行完更多任务之后,调用这个notyfyDidMore() 才会隐藏加载圈等操作
*/
public void notifyDidMore() {
mUIHandler.sendEmptyMessage(WHAT_DID_MORE);
}
/** 刷新完毕 关闭头部滚动条 **/
public void RefreshComplete() {
mUIHandler.sendEmptyMessage(WHAT_DID_REFRESH);
}
/**
* 设置监听器
*
* @param listener
*/
public void setOnPullDownListener(OnPullDownListener listener) {
mOnPullDownListener = listener;
}
/**
* 获取内嵌的listview
*
* @return ScrollOverListView
*/
public ListView getListView() {
return mListView;
}
/**
* 是否开启自动获取更多 自动获取更多,将会隐藏footer,并在到达底部的时候自动刷新
*
* @param index
* 倒数第几个触发
*/
public void enableAutoFetchMore(boolean enable, int index) {
if (enable) {
mListView.setBottomPosition(index);
mFooterLoadingView.setVisibility(View.GONE);
} else {
mFooterTextView.setText("数据加载完毕");
mFooterLoadingView.setVisibility(View.GONE);
}
mEnableAutoFetchMore = enable;
}
/**
* Private method 具体实现下拉刷新等操作
*/
/**
* 初始化界面
*/
private void initHeaderViewAndFooterViewAndListView(Context context) {
setOrientation(LinearLayout.VERTICAL);
/**
* 自定义脚部文件
*/
mFooterView = (RelativeLayout) LayoutInflater.from(context).inflate(
R.layout.pulldown_footer, null);
mFooterTextView = (TextView) mFooterView
.findViewById(R.id.pulldown_footer_text);
mFooterLoadingView = (ProgressBar) mFooterView
.findViewById(R.id.pulldown_footer_loading);
// mFooterView.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// if (!mIsFetchMoreing) {
//
// mIsFetchMoreing = true;
// mFooterTextView.setText("加载更多中...");
// mFooterLoadingView.setVisibility(View.VISIBLE);
// mOnPullDownListener.onMore();
// }
// }
// });
/**
* ScrollOverListView 同样是考虑到都是使用,所以放在这里 同时因为,需要它的监听事件
*/
mListView = new ScrollOverListView(context);
mListView.setOnScrollOverListener(this);
mListView.setCacheColorHint(0);
addView(mListView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
// 空的listener
mOnPullDownListener = new OnPullDownListener() {
@Override
public void onRefresh() {
}
@Override
public void onMore() {
}
};
mListView.addFooterView(mFooterView);
mListView.setFooterDividersEnabled(false);
//mListView.setAdapter(mListView.getAdapter());
}
private Handler mUIHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case WHAT_DID_REFRESH: {
mListView.onRefreshComplete();
return;
}
case WHAT_DID_MORE: {
mIsFetchMoreing = false;
mFooterTextView.setText("加载更多");
mFooterLoadingView.setVisibility(View.GONE);
}
}
}
};
/**
* 条目是否填满整个屏幕
*/
private boolean isFillScreenItem() {
final int firstVisiblePosition = mListView.getFirstVisiblePosition();
final int lastVisiblePostion = mListView.getLastVisiblePosition()
- mListView.getFooterViewsCount();
final int visibleItemCount = lastVisiblePostion - firstVisiblePosition
+ 1;
final int totalItemCount = mListView.getCount()
- mListView.getFooterViewsCount();
if (visibleItemCount < totalItemCount)
return true;
return false;
}
/*
* ================================== 实现 OnScrollOverListener接口
*/
@Override
public boolean onListViewTopAndPullDown(int delta) {
return true;
}
@Override
public boolean onListViewBottomAndPullUp(int delta) {
if (!mEnableAutoFetchMore || mIsFetchMoreing)
return false;
/** 数量充满屏幕才触发*/
if (isFillScreenItem()) {
mIsFetchMoreing = true;
mFooterTextView.setText("加载更多中...");
mFooterLoadingView.setVisibility(View.VISIBLE);
mOnPullDownListener.onMore();
return true;
}
return false;
}
@Override
public boolean onMotionDown(MotionEvent ev) {
mIsPullUpDone = false;
mMotionDownLastY = ev.getRawY();
return false;
}
@Override
public boolean onMotionMove(MotionEvent ev, int delta) {
/** 当头部文件回推消失的时候,不允许滚动*/
if (mIsPullUpDone)
return true;
/** 如果开始按下到滑动距离不超过误差值,则不滑动*/
final int absMotionY = (int) Math.abs(ev.getRawY() - mMotionDownLastY);
if (absMotionY < START_PULL_DEVIATION)
return true;
return false;
}
@Override
public boolean onMotionUp(MotionEvent ev) {
if (ScrollOverListView.canRefleash) {
ScrollOverListView.canRefleash = false;
mOnPullDownListener.onRefresh();
}
return false;
}
/**隐藏头部 禁用下拉更新**/
public void setHideHeader() {
mListView.showRefresh = false;
}
/**显示头部 使用下拉更新**/
public void setShowHeader() {
mListView.showRefresh = true;
}
/**隐藏底部 禁用上拉更多**/
public void setHideFooter() {
mFooterView.setVisibility(View.GONE);
mFooterTextView.setVisibility(View.GONE);
mFooterLoadingView.setVisibility(View.GONE);
enableAutoFetchMore(false, 1);
}
/**显示底部 使用上拉更多**/
public void setShowFooter() {
mFooterView.setVisibility(View.VISIBLE);
mFooterTextView.setVisibility(View.VISIBLE);
mFooterLoadingView.setVisibility(View.VISIBLE);
enableAutoFetchMore(true, 1);
}
}
3、实际操作
代码实例:
/**
* 下拉刷新上拉更多
*
* @author
*/
public class PullDownActivity extends Activity implements OnPullDownListener,
OnItemClickListener {
/** Handler What加载数据完毕 **/
private static final int WHAT_DID_LOAD_DATA = 0;
/** Handler What更新数据完毕 **/
private static final int WHAT_DID_REFRESH = 1;
/** Handler What更多数据完毕 **/
private static final int WHAT_DID_MORE = 2;
private ListView mListView;
private ArrayAdapter<String> mAdapter;
private PullDownView mPullDownView;
private List<String> mStrings = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pulldown);
/**
* 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
*/
mPullDownView = (PullDownView) findViewById(R.id.pull_down_view);
mPullDownView.setOnPullDownListener(this);
mListView = mPullDownView.getListView();
mListView.setOnItemClickListener(this);
mAdapter = new ArrayAdapter<String>(this, R.layout.pulldown_item,
mStrings);
mListView.setAdapter(mAdapter);
// 设置可以自动获取更多 滑到最后一个自动获取 改成false将禁用自动获取更多
mPullDownView.enableAutoFetchMore(true, 1);
// 隐藏 并禁用尾部
mPullDownView.setHideFooter();
// 显示并启用自动获取更多
mPullDownView.setShowFooter();
/** 隐藏并且禁用头部刷新*/
mPullDownView.setHideHeader();
/** 显示并且可以使用头部刷新*/
mPullDownView.setShowHeader();
/** 之前 网上很多代码 都会导致刷新事件 跟 上下文菜单同时弹出 这里做测试。。。已经解决 */
mListView
.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
}
});
// 加载数据 本类使用
loadData();
}
/** 刷新事件接口 这里要注意的是获取更多完 要关闭 刷新的进度条RefreshComplete() **/
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/** 关闭 刷新完毕 ***/
mPullDownView.RefreshComplete();// 这个事线程安全的 可看源代码
// 这个地方有点疑问
Message msg = mUIHandler.obtainMessage(WHAT_DID_REFRESH);
msg.obj = "After refresh " + System.currentTimeMillis();
msg.sendToTarget();
}
}).start();
}
/** 刷新事件接口 这里要注意的是获取更多完 要关闭 更多的进度条 notifyDidMore() **/
@Override
public void onMore() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 告诉它获取更多完毕 这个事线程安全的 可看源代码
mPullDownView.notifyDidMore();
Message msg = mUIHandler.obtainMessage(WHAT_DID_MORE);
msg.obj = "After more " + System.currentTimeMillis();
msg.sendToTarget();
}
}).start();
}
private Handler mUIHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case WHAT_DID_LOAD_DATA: {
if (msg.obj != null) {
List<String> strings = (List<String>) msg.obj;
if (!strings.isEmpty()) {
mStrings.addAll(strings);
mAdapter.notifyDataSetChanged();
}
}
Toast.makeText(PullDownActivity.this,
"" + mListView.getAdapter().getCount(),
Toast.LENGTH_LONG).show();
// 诉它数据加载完毕;
break;
}
case WHAT_DID_REFRESH: {
String body = (String) msg.obj;
mStrings.add(0, body);
mAdapter.notifyDataSetChanged();
// 告诉它更新完毕
break;
}
case WHAT_DID_MORE: {
String body = (String) msg.obj;
mStrings.add(body);
mAdapter.notifyDataSetChanged();
break;
}
}
}
};
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(this, "啊,你点中我了 " + position, Toast.LENGTH_SHORT).show();
}
// 模拟数据
private String[] mStringArray = { "Abbaye de Belloc"/*
* ,
* "Abbaye du Mont des Cats"
* , "Abertam",
* "Abondance",
* "Ackawi", "Acorn",
* "Adelost",
* "Affidelice au Chablis"
* , "Afuega'l Pitu",
* "Airag", "Airedale",
* "Aisy Cendre",
* "Allgauer Emmentaler"
* , "Alverca",
* "Ambert",
* "American Cheese"
*/};
private void loadData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(0000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<String> strings = new ArrayList<String>();
for (String body : mStringArray) {
strings.add(body);
}
Message msg = mUIHandler.obtainMessage(WHAT_DID_LOAD_DATA);
msg.obj = strings;
msg.sendToTarget();
}
}).start();
}
}
4、零碎小东西
资源文件
pull_down_head.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- ListView的头部 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- 内容 -->
<RelativeLayout
android:id="@+id/head_contentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp" >
<!-- 箭头图像、进度条 -->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<!-- 箭头 -->
<ImageView
android:id="@+id/head_arrowImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/pull_down_arrow" />
<!-- 进度条 -->
<ProgressBar
android:id="@+id/head_progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
<!-- 提示、最近更新 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
<!-- 提示 -->
<TextView
android:id="@+id/head_tipsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉刷新"
android:textColor="#000000"
android:textSize="20sp" />
<!-- 最近更新 -->
<TextView
android:id="@+id/head_lastUpdatedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上次更新"
android:textColor="#000000"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
pull_down_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/pulldown_footer_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="更多"
android:textSize="15dp" />
<ProgressBar
android:id="@+id/pulldown_footer_loading"
style="@android :style/Widget.ProgressBar.Small.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:visibility="gone" />
</RelativeLayout>