视频教程传送门:http://www.imooc.com/learn/135
完整代码在最下面。。
头布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/txt_xia_la"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉刷新"
android:textSize="18sp" />
<TextView
android:id="@+id/txt_last_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="@+id/jian_tou"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:layout_toLeftOf="@+id/layout"
android:src="@mipmap/up" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:layout_toLeftOf="@+id/layout"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
添加头布局并隐藏
private void initView(Context context) {
header = LayoutInflater.from(context).inflate(R.layout.refresh_header, null);
measureView(header);
headerHeight = header.getMeasuredHeight();
topPadding(-headerHeight);
addHeaderView(header);
setOnScrollListener(this);
}
设置上边距
/**
* 设置header上边距
*
* @param topPadding 上边距
*/
private void topPadding(int topPadding) {
header.setPadding(header.getPaddingLeft(), topPadding,
header.getPaddingRight(), header.getPaddingBottom());
header.invalidate();
}
隐藏头布局得告诉父布局头布局占多大宽高
/**
* 通知父布局占多大宽高
*/
private void measureView(View view) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if (layoutParams == null) {
layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width = ViewGroup.getChildMeasureSpec(0, 0, layoutParams.width);
int height;
int tempHeight = layoutParams.height;
if (tempHeight > 0) {
// 高度不是0的时候,要填充
height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
} else {
// 高度是0的时候不要填充
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
}
给布局添加滚动事件
/**
* 滚动事件
*
* @param view 当前可见的view
* @param firstVisibleItem 当前界面第一个可见的item的位置
* @param visibleItemCount
* @param totalItemCount
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
this.firstVisibleItem = firstVisibleItem;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
}
按下时的三个事件
/**
* 触摸事件
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 判断界面是不是在最顶端
if (firstVisibleItem == 0) {
isRemark = true;
startY = (int) ev.getY();
}
break;
case MotionEvent.ACTION_MOVE:
onMove(ev);
break;
case MotionEvent.ACTION_UP:
if (state == RELESE) {
state = REFRESHING;
//加载最新数据
refreshViewByState();
mListener.onRefresh();
} else if (state == PULL) {
state = NONE;
isRemark = false;
refreshViewByState();
}
break;
}
return super.onTouchEvent(ev);
}
手指移动的操作
/**
* 判断移动过程中的操作
*/
private void onMove(MotionEvent ev) {
if (!isRemark) {
return;
}
//当前已经移动到什么位置
int tempY = (int) ev.getY();
//手指移动的距离
int space = tempY - startY;
int topPadding = space - headerHeight;
switch (state) {
case NONE:
if (space > 0) {
state = PULL;
refreshViewByState();
}
break;
case PULL:
topPadding(topPadding);
if (space > headerHeight + 30 && scrollState == SCROLL_STATE_TOUCH_SCROLL) {
state = RELESE;
refreshViewByState();
}
break;
case RELESE:
topPadding(topPadding);
if (space < headerHeight + 30) {
state = PULL;
refreshViewByState();
} else if (space <= 0) {
state =