RecyclerView下拉刷新实现
实现分析:
1.通过topMargin实现
2.通过改变头部高度实现
这里介绍的使用 第二种方法实现:通过滑动监听动态的改变头部高度实现。
1.初始化相关参数
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!load) {
//滑动最大高度
scroll_height = getHeight() / 3;
//头部的高度
viewHolder_height = getHeight() / 5;
//得到头部
viewHolder = (ViewGroup) getChildAt(0);
//头部布局参数
params = (ViewGroup.LayoutParams) viewHolder.getLayoutParams();
params.height = 0;
viewHolder.setLayoutParams(params);
//头部 刷新文字
textView = (TextView) viewHolder.findViewById(R.id.textView);
textView.setText("测试。。。。");
//得到下拉刷新控件
rv = (RecyclerView) getChildAt(1);
rv.setOnTouchListener(this);
load = true;
}
}
2.RecyclerView滑动监听
/*
处理RecyclerView触摸事件;
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
if (current_Status == START_REFRESHING) {
//正在刷新时屏蔽状态
return true;
}
startDown();
if (start_Drop_Down) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
down_Y = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float move_Y = event.getRawY();
//判断是否下拉
float difference = move_Y - down_Y;
//difference>0:下滑,difference<=0表示上滑
if (difference <= 0) {
params.height = 0;
viewHolder.setLayoutParams(params);
return false;
}
//不是正在刷新
if (current_Status != START_REFRESHING) {
//达到头部一半就表示达到刷新条件
if (params.height >= viewHolder_height / 2) {
//达到下拉刷新要求的状态
current_Status = STATE_REFRESHING;
//更新文本
textView.setText("放手刷新。。。");
} else {
//刷新结束状态(包括刷新完和未达到刷新要求)
current_Status = STATE_DROP_DOWN;
//更新文本
textView.setText("下拉刷新。。。");
}
params.height = (int) (difference / 3);
if (params.height >= scroll_height) {
params.height = scroll_height;
}
viewHolder.setLayoutParams(params);
}
break;
case MotionEvent.ACTION_UP:
if (params.height >= viewHolder_height / 2) {
current_Status = STATE_REFRESHING;
} else {
current_Status = STATE_DROP_DOWN;
}
if (current_Status == STATE_REFRESHING) {
//执行下拉刷新
new HeadOperation().execute();
} else if (current_Status == STATE_DROP_DOWN) {
current_Status = END_REFRESHING;
//执行下拉刷新结束后的操作
new HeadOperation().execute();
}
break;
}
if (current_Status == STATE_REFRESHING || current_Status == STATE_DROP_DOWN) {
return true;
}
}
return false;
}
3、头部的隐藏和显示操作
//头部的操作:隐藏,显示
class HeadOperation extends AsyncTask<Void, Integer, Integer> {
@Override
protected Integer doInBackground(Void... param) {
int height = params.height;
//显示头部
if (current_Status == STATE_REFRESHING) {
while (true) {//让头部全部露出来
height = height + 50;
if (height >= viewHolder_height) {
height = viewHolder_height;
break;
}
publishProgress(height);
SystemClock.sleep(10);
}
//改变状态:正在刷新
current_Status = START_REFRESHING;
//改变UI更新状态
update_ui = true;
publishProgress(height);
//刷新中执行的操作
if (listener != null) {
listener.onRefreshing();
} else {
//默认刷新时间
SystemClock.sleep(5000);
//改变状态:结束刷新
while (true) {
height = height - 70;
if (height <= 0) {
height = 0;
break;
}
publishProgress(height);
SystemClock.sleep(10);
}
current_Status = END_REFRESHING;
publishProgress(height);
return 1;
}
} else if (current_Status == END_REFRESHING) {//隐藏头部
while (true) {
height = height - 70;
if (height <= 0) {
height = 0;
break;
}
publishProgress(height);
SystemClock.sleep(10);
}
publishProgress(height);
return 1;
}
return -1;
}
@Override
protected void onProgressUpdate(Integer... values) {
params.height = values[0];
viewHolder.setLayoutParams(params);
if (current_Status == START_REFRESHING) {
if (textView != null)
textView.setText("正在刷新。。。。");
}
}
@Override
protected void onPostExecute(Integer aVoid) {
if (current_Status == END_REFRESHING) {
if (listener != null && update_ui) {
//刷新完成后的操作,如提示信息什么的
listener.onEndRefreshing();
update_ui = false;
}
}
}
}
4、对外的刷新的接口
//实现刷新监听的方法
public void setOnRefreshListener(OnRefreshListener listener) {
this.listener = listener;
}
//刷新的监听
public interface OnRefreshListener {
//正在刷新:子线程
void onRefreshing();
//停止刷新:主线程
void onEndRefreshing();
}
//提供一个外部停止刷新的方法
public void setEndRefreshing() {
current_Status = END_REFRESHING;
new HeadOperation().execute();
}
5、Activity中
recyclerView_Refresh.setOnRefreshListener(new RecyclerView_Refresh.OnRefreshListener() {
@Override
public void onRefreshing() {//正在刷新
SystemClock.sleep(3000);
//停止刷新
recyclerView_Refresh.setEndRefreshing();
}
@Override
public void onEndRefreshing() {//结束刷新
Toast.makeText(MainActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
}
});
需要不同头部动画的自行修改,已做注释;
下载地址