5
5
import android .support .v4 .view .MotionEventCompat ;
6
6
import android .support .v4 .view .ViewCompat ;
7
7
import android .util .AttributeSet ;
8
+ import android .util .Log ;
8
9
import android .util .TypedValue ;
9
10
import android .view .MotionEvent ;
10
11
import android .view .View ;
@@ -56,6 +57,7 @@ public class PullRefreshLayout extends ViewGroup {
56
57
57
58
public int mDurationToStartPosition ;
58
59
public int mDurationToCorrectPosition ;
60
+ private int mInitialOffsetTop ;
59
61
60
62
public PullRefreshLayout (Context context ) {
61
63
this (context , null );
@@ -86,14 +88,14 @@ public PullRefreshLayout(Context context, AttributeSet attrs) {
86
88
ViewCompat .setChildrenDrawingOrderEnabled (this , true );
87
89
}
88
90
89
- public void setColorSchemeColors (int [] colorSchemeColors ){
91
+ public void setColorSchemeColors (int [] colorSchemeColors ) {
90
92
mColorSchemeColors = colorSchemeColors ;
91
93
mRefreshDrawable .setColorSchemeColors (colorSchemeColors );
92
94
}
93
95
94
- public void setRefreshStyle (int type ){
96
+ public void setRefreshStyle (int type ) {
95
97
setRefreshing (false );
96
- switch (type ){
98
+ switch (type ) {
97
99
case STYLE_MATERIAL :
98
100
mRefreshDrawable = new MaterialDrawable (getContext (), this );
99
101
break ;
@@ -113,14 +115,14 @@ public void setRefreshStyle(int type){
113
115
mRefreshView .setImageDrawable (mRefreshDrawable );
114
116
}
115
117
116
- public void setRefreshDrawable (RefreshDrawable drawable ){
118
+ public void setRefreshDrawable (RefreshDrawable drawable ) {
117
119
setRefreshing (false );
118
120
mRefreshDrawable = drawable ;
119
121
mRefreshDrawable .setColorSchemeColors (mColorSchemeColors );
120
122
mRefreshView .setImageDrawable (mRefreshDrawable );
121
123
}
122
124
123
- public int getFinalOffset (){
125
+ public int getFinalOffset () {
124
126
return mSpinnerFinalOffset ;
125
127
}
126
128
@@ -154,22 +156,30 @@ private void ensureTarget() {
154
156
@ Override
155
157
public boolean onInterceptTouchEvent (MotionEvent ev ) {
156
158
157
- if (!isEnabled () || canChildScrollUp () || mRefreshing ) {
159
+ if (!isEnabled () || canChildScrollUp ()) {
158
160
return false ;
159
161
}
160
162
163
+ // if (mRefreshing) {
164
+ // Log.d("byz", "mCurrentOffsetTop " + mCurrentOffsetTop);
165
+ // return mCurrentOffsetTop > 0;
166
+ // }
167
+
161
168
final int action = MotionEventCompat .getActionMasked (ev );
162
169
163
170
switch (action ) {
164
171
case MotionEvent .ACTION_DOWN :
165
- setTargetOffsetTop (0 , true );
172
+ if (!mRefreshing ) {
173
+ setTargetOffsetTop (0 , true );
174
+ }
166
175
mActivePointerId = MotionEventCompat .getPointerId (ev , 0 );
167
176
mIsBeingDragged = false ;
168
177
final float initialMotionY = getMotionEventY (ev , mActivePointerId );
169
178
if (initialMotionY == -1 ) {
170
179
return false ;
171
180
}
172
181
mInitialMotionY = initialMotionY ;
182
+ mInitialOffsetTop = mCurrentOffsetTop ;
173
183
break ;
174
184
case MotionEvent .ACTION_MOVE :
175
185
if (mActivePointerId == INVALID_POINTER ) {
@@ -184,7 +194,14 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
184
194
// mIsBeingDragged = true;
185
195
// mMode = yDiff > 0 ? MODE_TOP : MODE_BOTTOM;
186
196
// }
187
- if (yDiff > mTouchSlop && !mIsBeingDragged ) {
197
+ if (mRefreshing ) {
198
+ mIsBeingDragged = !(yDiff < 0 && mCurrentOffsetTop <= 0 );
199
+ // if (yDiff > 0) {
200
+ // mIsBeingDragged = mCurrentOffsetTop <= 0;
201
+ // } else {
202
+ // mIsBeingDragged = mCurrentOffsetTop >= 0;
203
+ // }
204
+ } else if (yDiff > mTouchSlop && !mIsBeingDragged ) {
188
205
mIsBeingDragged = true ;
189
206
}
190
207
break ;
@@ -220,27 +237,37 @@ public boolean onTouchEvent(MotionEvent ev) {
220
237
final float y = MotionEventCompat .getY (ev , pointerIndex );
221
238
// final float yDiff = Math.abs(y - mInitialMotionY);
222
239
final float yDiff = y - mInitialMotionY ;
223
- final float scrollTop = yDiff * DRAG_RATE ;
224
- float originalDragPercent = scrollTop / mTotalDragDistance ;
225
- if (originalDragPercent < 0 ) {
226
- return false ;
227
- }
228
- float dragPercent = Math .min (1f , Math .abs (originalDragPercent ));
240
+ int targetY ;
241
+ if (mRefreshing ) {
242
+ targetY = (int ) (mInitialOffsetTop + yDiff );
243
+ if (targetY < 0 ) {
244
+ targetY = 0 ;
245
+ } else if (targetY > mTotalDragDistance ) {
246
+ targetY = mTotalDragDistance ;
247
+ }
248
+ } else {
249
+ final float scrollTop = yDiff * DRAG_RATE ;
250
+ float originalDragPercent = scrollTop / mTotalDragDistance ;
251
+ if (originalDragPercent < 0 ) {
252
+ return false ;
253
+ }
254
+ float dragPercent = Math .min (1f , Math .abs (originalDragPercent ));
229
255
// float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
230
256
// float adjustedPercent = dragPercent;
231
- float extraOS = Math .abs (scrollTop ) - mTotalDragDistance ;
232
- float slingshotDist = mSpinnerFinalOffset ;
233
- float tensionSlingshotPercent = Math .max (0 ,
234
- Math .min (extraOS , slingshotDist * 2 ) / slingshotDist );
235
- float tensionPercent = (float ) ((tensionSlingshotPercent / 4 ) - Math .pow (
236
- (tensionSlingshotPercent / 4 ), 2 )) * 2f ;
237
- float extraMove = (slingshotDist ) * tensionPercent * 2 ;
238
- int targetY = (int ) ((slingshotDist * dragPercent ) + extraMove );
239
- if (mRefreshView .getVisibility () != View .VISIBLE ) {
240
- mRefreshView .setVisibility (View .VISIBLE );
241
- }
242
- if (scrollTop < mTotalDragDistance ) {
243
- mRefreshDrawable .setPercent (dragPercent );
257
+ float extraOS = Math .abs (scrollTop ) - mTotalDragDistance ;
258
+ float slingshotDist = mSpinnerFinalOffset ;
259
+ float tensionSlingshotPercent = Math .max (0 ,
260
+ Math .min (extraOS , slingshotDist * 2 ) / slingshotDist );
261
+ float tensionPercent = (float ) ((tensionSlingshotPercent / 4 ) - Math .pow (
262
+ (tensionSlingshotPercent / 4 ), 2 )) * 2f ;
263
+ float extraMove = (slingshotDist ) * tensionPercent * 2 ;
264
+ targetY = (int ) ((slingshotDist * dragPercent ) + extraMove );
265
+ if (mRefreshView .getVisibility () != View .VISIBLE ) {
266
+ mRefreshView .setVisibility (View .VISIBLE );
267
+ }
268
+ if (scrollTop < mTotalDragDistance ) {
269
+ mRefreshDrawable .setPercent (dragPercent );
270
+ }
244
271
}
245
272
setTargetOffsetTop (targetY - mCurrentOffsetTop , true );
246
273
break ;
@@ -254,7 +281,7 @@ public boolean onTouchEvent(MotionEvent ev) {
254
281
break ;
255
282
case MotionEvent .ACTION_UP :
256
283
case MotionEvent .ACTION_CANCEL : {
257
- if (mActivePointerId == INVALID_POINTER ) {
284
+ if (mActivePointerId == INVALID_POINTER || mRefreshing ) {
258
285
return false ;
259
286
}
260
287
final int pointerIndex = MotionEventCompat .findPointerIndex (ev , mActivePointerId );
@@ -410,8 +437,8 @@ private void setTargetOffsetTop(int offset, boolean requiresUpdate) {
410
437
mRefreshView .bringToFront ();
411
438
// mRefreshView.offsetTopAndBottom(offset);
412
439
mTarget .offsetTopAndBottom (offset );
413
- mRefreshDrawable .offsetTopAndBottom (offset );
414
440
mCurrentOffsetTop = mTarget .getTop ();
441
+ mRefreshDrawable .offsetTopAndBottom (offset );
415
442
if (requiresUpdate && android .os .Build .VERSION .SDK_INT < 11 ) {
416
443
invalidate ();
417
444
}
@@ -461,7 +488,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
461
488
int right = getPaddingRight ();
462
489
int bottom = getPaddingBottom ();
463
490
464
- mTarget .layout (left , top + mCurrentOffsetTop , left + width - right , top + height - bottom + mCurrentOffsetTop );
491
+ mTarget .layout (left , top + mTarget . getTop () , left + width - right , top + height - bottom + mTarget . getTop () );
465
492
// mRefreshView.layout(width / 2 - mRefreshViewWidth / 2, -mRefreshViewHeight + mCurrentOffsetTop, width / 2 + mRefreshViewHeight / 2, mCurrentOffsetTop);
466
493
mRefreshView .layout (left , top , left + width - right , top + height - bottom );
467
494
}
0 commit comments