-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScrollableLayout.java
433 lines (380 loc) · 14.6 KB
/
ScrollableLayout.java
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package com.risenb.honourfamily.views.scrollablelayout;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;
import com.zhy.autolayout.AutoLayoutInfo;
import com.zhy.autolayout.utils.AutoLayoutHelper;
/**
* <pre>
* author : linzheng
* e-mail : [email protected]
* time : 2017/05/03
* desc : 嵌套滑动布局
* version: 1.0
* </pre>
*/
public class ScrollableLayout extends LinearLayout {
private AutoLayoutHelper mAutoLayoutHelper = new AutoLayoutHelper(this);
private final String tag = "cp:scrollableLayout";
private float mDownX;
private float mDownY;
private float mLastY;
private int minY = 0;
private int maxY = 0;
private int mHeadHeight;
private int mExpandHeight;
private int mTouchSlop;
private int mMinimumVelocity;
private int mMaximumVelocity;
// 方向
private DIRECTION mDirection;
private int mCurY;
private int mLastScrollerY;
private boolean needCheckUpdown;
private boolean updown;
private boolean mDisallowIntercept;
private boolean isClickHead;
private boolean isClickHeadExpand;
private View mHeadView;
private ViewPager childViewPager;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
/**
* 滑动方向 *
*/
enum DIRECTION {
UP,// 向上划
DOWN// 向下划
}
public interface OnScrollListener {
void onScroll(int currentY, int maxY);
}
private OnScrollListener onScrollListener;
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
private ScrollableHelper mHelper;
public ScrollableHelper getHelper() {
return mHelper;
}
public ScrollableLayout(Context context) {
super(context);
init(context);
}
public ScrollableLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public ScrollableLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ScrollableLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context) {
//初始化ScrollableHelper
mHelper = new ScrollableHelper();
//初始化Scroller
mScroller = new Scroller(context);
//一些常量的初始化
final ViewConfiguration configuration = ViewConfiguration.get(context);
mTouchSlop = configuration.getScaledTouchSlop();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
//表示HeadView 是否还显示
//ture 不显示 false 显示
public boolean isSticked() {
return mCurY == maxY;
}
/**
* 扩大头部点击滑动范围
*/
public void setClickHeadExpand(int expandHeight) {
mExpandHeight = expandHeight;
}
public int getMaxY() {
return maxY;
}
public boolean isHeadTop() {
return mCurY == minY;
}
public boolean canPtr() {
return updown && mCurY == minY && mHelper.isTop();
}
public void requestScrollableLayoutDisallowInterceptTouchEvent(boolean disallowIntercept) {
super.requestDisallowInterceptTouchEvent(disallowIntercept);
mDisallowIntercept = disallowIntercept;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//分发事件
float currentX = ev.getX();
float currentY = ev.getY();
float deltaY;
int shiftX = (int) Math.abs(currentX - mDownX);
int shiftY = (int) Math.abs(currentY - mDownY);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDisallowIntercept = false;
needCheckUpdown = true;
updown = true;
mDownX = currentX;
mDownY = currentY;
mLastY = currentY;
checkIsClickHead((int) currentY, mHeadHeight, getScrollY());
checkIsClickHeadExpand((int) currentY, mHeadHeight, getScrollY());
//初始化速度追踪器
initOrResetVelocityTracker();
mVelocityTracker.addMovement(ev);
//强制停止
mScroller.forceFinished(true);
break;
case MotionEvent.ACTION_MOVE:
//不准拦截事件
if (mDisallowIntercept) {
break;
}
//初始化速度追踪器
initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);
//Y轴滑动距离
deltaY = mLastY - currentY;
if (needCheckUpdown) {
if (shiftX > mTouchSlop && shiftX > shiftY) {
needCheckUpdown = false;
updown = false;
} else if (shiftY > mTouchSlop && shiftY > shiftX) {
needCheckUpdown = false;
updown = true;
}
}
//needCheckUpdown 表示符 用来表示是否需要判断 updown,当滑动的距离大于最小滑动距离 且当x移动距离大于y移动距离或者y大于x的时候就不需要判断updown了
//updown 表示 当前滑动是否是上下滑动 x 移动大于 y 这里判定为横向滑动所以 updown = false, y 大于 x 表示是横向滑动 所以 updown = true 这时候才会走下面的逻辑
//判断是否需要 滑动 ScrollableLayout
//需要满足下面几个条件
// updown == true
// y 移动大于最下距离
// y 移动大于 x 移动
// HeadView没有隐藏
if (updown && shiftY > mTouchSlop && shiftY > shiftX &&
(!isSticked() || mHelper.isTop() || isClickHeadExpand)) {
//满足上面的条件就 设置ViewPager不允许拦截事件
if (childViewPager != null) {
childViewPager.requestDisallowInterceptTouchEvent(true);
}
//
scrollBy(0, (int) (deltaY + 0.5));
}
mLastY = currentY;
break;
case MotionEvent.ACTION_UP:
//判断 上下滑动 y移动大于x移动 y 大于最小滑动距离
if (updown && shiftY > shiftX && shiftY > mTouchSlop) {
//设置速度跟踪器
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
//Y轴的速度
float yVelocity = -mVelocityTracker.getYVelocity();
boolean dislowChild = false;
//速度是否大于最小速度
if (Math.abs(yVelocity) > mMinimumVelocity) {
//如果速度是正数表示向上滑动 负数表示向下滑动
mDirection = yVelocity > 0 ? DIRECTION.UP : DIRECTION.DOWN;
if ((mDirection == DIRECTION.UP && isSticked()) || (!isSticked() && getScrollY() == 0 && mDirection == DIRECTION.DOWN)) {
dislowChild = true;
} else {
//使用scroller 实现 fling
mScroller.fling(0, getScrollY(), 0, (int) yVelocity, 0, 0, -Integer.MAX_VALUE, Integer.MAX_VALUE);
mScroller.computeScrollOffset();
mLastScrollerY = getScrollY();
invalidate();
}
}
if (!dislowChild && (isClickHead || !isSticked())) {
int action = ev.getAction();
ev.setAction(MotionEvent.ACTION_CANCEL);
boolean dispathResult = super.dispatchTouchEvent(ev);
ev.setAction(action);
return dispathResult;
}
}
break;
default:
break;
}
super.dispatchTouchEvent(ev);
return true;
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private int getScrollerVelocity(int distance, int duration) {
if (mScroller == null) {
return 0;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return (int) mScroller.getCurrVelocity();
} else {
return distance / duration;
}
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
final int currY = mScroller.getCurrY();
//将事件传递给内容View
if (mDirection == DIRECTION.UP) {
// 手势向上划
if (isSticked()) {
int distance = mScroller.getFinalY() - currY;
int duration = calcDuration(mScroller.getDuration(), mScroller.timePassed());
mHelper.smoothScrollBy(getScrollerVelocity(distance, duration), distance, duration);
mScroller.forceFinished(true);
return;
} else {
//如果HeadView还显示 那么只会滚动ScrollableLayout
scrollTo(0, currY);
}
} else {
// 手势向下划
if (mHelper.isTop() || isClickHeadExpand) {
int deltaY = (currY - mLastScrollerY);
int toY = getScrollY() + deltaY;
scrollTo(0, toY);
if (mCurY <= minY) {
mScroller.forceFinished(true);
return;
}
}
invalidate();
}
mLastScrollerY = currY;
}
}
@Override
public void scrollBy(int x, int y) {
int scrollY = getScrollY();
int toY = scrollY + y;
if (toY >= maxY) {
toY = maxY;
} else if (toY <= minY) {
toY = minY;
}
y = toY - scrollY;
super.scrollBy(x, y);
}
@Override
public void scrollTo(int x, int y) {
if (y >= maxY) {
y = maxY;
} else if (y <= minY) {
y = minY;
}
mCurY = y;
if (onScrollListener != null) {
onScrollListener.onScroll(y, maxY);
}
super.scrollTo(x, y);
}
private void initOrResetVelocityTracker() {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
} else {
mVelocityTracker.clear();
}
}
private void initVelocityTrackerIfNotExists() {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
}
private void recycleVelocityTracker() {
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
private void checkIsClickHead(int downY, int headHeight, int scrollY) {
isClickHead = downY + scrollY <= headHeight;
}
private void checkIsClickHeadExpand(int downY, int headHeight, int scrollY) {
if (mExpandHeight <= 0) {
isClickHeadExpand = false;
}
isClickHeadExpand = downY + scrollY <= headHeight + mExpandHeight;
}
private int calcDuration(int duration, int timepass) {
return duration - timepass;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!isInEditMode()) {
mAutoLayoutHelper.adjustChildren();
}
mHeadView = getChildAt(0);
//测量子HeadView
measureChildWithMargins(mHeadView, widthMeasureSpec, 0, MeasureSpec.UNSPECIFIED, 0);
maxY = mHeadView.getVisibility() == View.GONE ? 0 : mHeadView.getMeasuredHeight();
mHeadHeight = mHeadView.getVisibility() == View.GONE ? 0 : mHeadView.getMeasuredHeight();
//底部ViewPager的高度 = ScrollableLayout 剩余高度 + maxY/mHeadHeight
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) + maxY, MeasureSpec.EXACTLY));
}
@Override
protected void onFinishInflate() {
//设置HeadVIew可点击
if (mHeadView != null && !mHeadView.isClickable()) {
mHeadView.setClickable(true);
}
//循环获取ViewPager
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = getChildAt(i);
if (childAt != null && childAt instanceof ViewPager) {
childViewPager = (ViewPager) childAt;
}
}
super.onFinishInflate();
}
@Override
public LinearLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends LinearLayout.LayoutParams
implements AutoLayoutHelper.AutoLayoutParams {
private AutoLayoutInfo mAutoLayoutInfo;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
}
public LayoutParams(int w, int h) {
super(w, h);
}
public LayoutParams(int w, int h, float initWeight) {
super(w, h, initWeight);
}
public LayoutParams(ViewGroup.LayoutParams p) {
super(p);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
public LayoutParams(LayoutParams source) {
this((MarginLayoutParams) source);
mAutoLayoutInfo = source.mAutoLayoutInfo;
}
@Override
public AutoLayoutInfo getAutoLayoutInfo() {
return mAutoLayoutInfo;
}
}
}