项目中我们经常有上拉、下拉刷新的需求,几乎所有的listView、RecyclerView都会伴随着上拉、下拉刷新的需求,如果我们使用一些开源控件,换了控件我们就要更新,现在我们自己撸起袖子写一个通用的刷新控件
思路:
- 写一个继承RelativeLayout的RefreshLayout
- 添加头尾控件作为刷新控件
- 通过事件分发来进行刷新操作
- 通过动画来控制控件移动
目的:让他的所有子控件都可以使用,哪怕是一个TextView
public class RefreshLayout extends RelativeLayout {
/**
* 滑动控件时拉去的速度比例
*/
private final int V_REFRESH = 2;
/**
* 是否是刷新过程
* true 是
* false 不是
* 为false的时候才可以进行刷新
*/
private boolean mIsRefreshDuring;
/**
* 可以进下拉刷新
*/
private boolean mCanDownPull;
/**
* 可以进行上拉刷新
*/
private boolean mCanUpPull;
/**
* 判断触摸后是否是初次移动
*/
private boolean mIsFirstMove;
/**
* y轴呢平移的距离
*/
private int mDistanceY;
/**
* 刷新接口对象
*/
private OnRefresh mOnRefresh;
/**
* 用于控制事件拦截的变量
*/
private boolean mCanIntercept;
private int mTouchSlop;
private int mDistance;
private LayoutParams mHeaderParams;
private View mHeaderView;
private View mFootView;
private int mHeaderMaxHeight;
private int mStartY;
private LayoutParams mFootParams;
private int mFootMaxHeight;
private PullCallBack mCallBack;
private View mChildView;
private ObjectAnimator mAnimator;
public RefreshLayout(Context context) {
super(context);
initData();
}
public RefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initData();
}
public RefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData();
}
/**
* 必须让头尾控件实现的接口
*/
public interface HeadAndFootCallBack {
void setAttribute();
void startPull();
void stopPull();
}
/**
* 必须让被拖动的控件子类实现
*/
public interface PullCallBack {
boolean canDownPull();
boolean canUpPull();
}
private void initData() {
setWillNotDraw(false);
}
/**
* 下拉刷新完成后必须使用该方法
*/
public void downPullFinish() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
((HeadAndFootCallBack) mHeaderView).stopPull();
}
/**
* 上拉完成后必须调用该方法
*/
public void upPullFinish() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
((HeadAndFootCallBack) mFootView).stopPull();
}
/**
* 自动下拉刷新
*/
public void autoDownPullForHead() {
postDelayed(new Runnable() {
@Override
public void run() {
mCanDownPull = true;
mCanUpPull = false;
mAnimator.setFloatValues(10, mHeaderMaxHeight);
mAnimator.start();
((HeadAndFootCallBack) mHeaderView).startPull();
mOnRefresh.onDownPullRefresh();
}
}, 500);
}
/**
* 自动下拉刷新
*/
public void autoUpPullForHead() {
postDelayed(new Runnable() {
@Override
public void run() {
mCanDownPull = false;
mCanUpPull = true;
mAnimator.setFloatValues(0, mFootMaxHeight);
mAnimator.start();
((HeadAndFootCallBack) mFootView).startPull();
mOnRefresh.onUpPullRefresh();
}
}, 500);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mCanIntercept;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.e("shen", "mIsRefreshDuring=" + mIsRefreshDuring);
if (mIsRefreshDuring) {
return super.dispatchTouchEvent(event);
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartY = (int) event.getY();
initPull();
break;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() == 1) {
int moveY = (int) event.getY();
mDistanceY = (moveY - mStartY) / V_REFRESH;
if (!mIsFirstMove && mDistanceY != 0 && mDistanceY < mTouchSlop) {
mCanDownPull = mDistanceY > 0;
mCanUpPull = !mCanDownPull;
mIsFirstMove = true;
}
if (mCanDownPull && mCallBack.canDownPull()) {
upDataForDownPull();
mChildView.setEnabled(false);
mCanIntercept = true;
}
if (mCanUpPull && mCallBack.canUpPull()) {
upDataForUpPull();
mChildView.setEnabled(false);
mCanIntercept = true;
}
mStartY = moveY;
}
break;
case MotionEvent.ACTION_UP:
mIsRefreshDuring = true;
mIsFirstMove = false;
if (mHeaderParams.height >= mHeaderMaxHeight) {
((HeadAndFootCallBack) mHeaderView).startPull();
mOnRefresh.onDownPullRefresh();
} else if (mFootParams.height >= mFootMaxHeight) {
((HeadAndFootCallBack) mFootView).startPull();
mOnRefresh.onUpPullRefresh();
} else if (mHeaderParams.height > 0 && mHeaderParams.height < mHeaderMaxHeight) {
releaseForDownFinished();
} else if (mFootParams.height > 0 && mFootParams.height < mFootMaxHeight) {
releaseForUpFinished();
} else {
mIsRefreshDuring = false;
mCanIntercept = false;
}
break;
}
super.dispatchTouchEvent(event);
return true;
}
/**
* 每次进行触摸都需要进行初始化
*/
private void initPull() {
mCanDownPull = false;
mCanUpPull = false;
}
/**
* 不需要进行上拉刷新
*/
private void releaseForUpFinished() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
}
/**
* 不需要进行下拉刷新
*/
private void releaseForDownFinished() {
mAnimator.setFloatValues(mChildView.getTranslationY(), 0);
mAnimator.start();
}
/**
* 上拉时处理手势
*/
private void upDataForUpPull() {
if (mDistanceY != 0) {
mFootParams.height -= mDistanceY;
if (mFootParams.height <= 0) {
mFootParams.height = 0;
}
if (mFootParams.height >= mFootMaxHeight) {
mFootParams.height = mFootMaxHeight;
}
mChildView.setTranslationY(-mFootParams.height);
mFootView.requestLayout();
}
}
/**
* 下拉时处理手势
*/
private void upDataForDownPull() {
if (mDistanceY != 0) {
mHeaderParams.height += mDistanceY;
if (mHeaderParams.height >= mHeaderMaxHeight) {
mHeaderParams.height = mHeaderMaxHeight;
}
if (mHeaderParams.height <= 0) {
mHeaderParams.height = 0;
}
mChildView.setTranslationY(mHeaderParams.height);
mHeaderView.requestLayout();
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mHeaderView = getChildAt(0);
if (!(mHeaderView instanceof HeadAndFootCallBack)) {
new IllegalStateException("HeaderView必须实现HeadAndFootCallBack接口");
}
((HeadAndFootCallBack) mHeaderView).setAttribute();
mHeaderParams = (LayoutParams) mHeaderView.getLayoutParams();
mHeaderParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mFootView = getChildAt(2);
if (!(mFootView instanceof HeadAndFootCallBack)) {
new IllegalStateException("FootView必须实现HeadAndFootCallBack接口");
}
((HeadAndFootCallBack) mFootView).setAttribute();
mFootParams = (LayoutParams) mFootView.getLayoutParams();
mFootParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mChildView = getChildAt(1);
if (!(mChildView instanceof HeadAndFootCallBack)) {
new IllegalStateException("ChildView必须实现PullCallBack接口");
}
mCallBack = (PullCallBack) getChildAt(1);
mAnimator = ObjectAnimator.ofFloat(mChildView, "translationY", 0);
mAnimator.setInterpolator(new DecelerateInterpolator());
mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int translationY = (int) mChildView.getTranslationY();
if (mCanUpPull) {
mFootParams.height = Math.abs(translationY);
mFootView.requestLayout();
} else if (mCanDownPull) {
mHeaderParams.height = Math.abs(translationY);
mHeaderView.requestLayout();
}
Log.e("shen", "translationY=" + translationY);
Log.e("shen", "mHeaderParams.height=" + mHeaderParams.height);
if (translationY == 0) {
mChildView.setEnabled(true);
mDistanceY = 0;
mIsRefreshDuring = false;
mCanIntercept = false;
} else {
mIsRefreshDuring = true;
}
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
mDistance = mTouchSlop * 5;
mHeaderMaxHeight = mHeaderParams.height;
mHeaderParams.height = 0;
mHeaderView.requestLayout();
mFootMaxHeight = mFootParams.height;
mFootParams.height = 0;
mFootView.requestLayout();
}
/**
* 下拉/上拉事件监听
*/
public interface OnRefresh {
/**
* 下拉刷新
*/
void onDownPullRefresh();
/**
* 上拉加载
*/
void onUpPullRefresh();
}
public void setOnRefresh(OnRefresh onRefresh) {
mOnRefresh = onRefresh;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
给他添加三个控件,头尾就是刷新头、尾,第二个就是正常显示的控件。必须让头尾实现HeadAndFootCallBack接口,来设置属性,通知开始刷新、结束刷新
难点: 现在来说下开发时遇到的难点
- 由于判断在dispatchTouchEvent中,导致如果该控件以及子控件都不消费该事件的话,就会造成事件不会发送到它,因为如果不消费DOWN事件的话,之后所有的事件都不会在进行接收。解决方式,让该控件onTouchEvent方法消返回true,当子控件不进行事件消费的话,就会返回由该控件消费,不会造成因DOWN事件不消费而无法接收到事件,导致dispatchTouchEvent也不消费事件
- 动画,动画就是我的伤痛,最近在学习估值器
这个控件自认为写的不错,通过他可以帮我们学习事件分发、动画、接口回调,也是有一定的学习意义