@@ -54,22 +54,18 @@ public class DraggerView extends FrameLayout {
54
54
private static final int INVALID_POINTER = -1 ;
55
55
56
56
private boolean runAnimationOnFinishInflate = true ;
57
- private boolean animationFinish = false ;
58
57
private boolean canSlide = true ;
59
58
private int activePointerId = INVALID_POINTER ;
60
59
private float verticalDragRange ;
61
60
private float horizontalDragRange ;
62
61
private float dragLimit ;
63
62
private float tension ;
64
63
private float friction ;
65
- private float progress ;
66
- private double val ;
67
64
68
65
private TypedArray attributes ;
69
66
private DraggerPosition dragPosition ;
70
67
71
68
private DraggerCallback draggerCallback ;
72
- private DraggerHelperCallback dragHelperCallback ;
73
69
private ViewDragHelper dragHelper ;
74
70
private View dragView ;
75
71
private View shadowView ;
@@ -90,6 +86,10 @@ public DraggerView(Context context, AttributeSet attrs, int defStyle) {
90
86
initializeAttributes (attrs );
91
87
}
92
88
89
+ /**
90
+ * Bind the attributes of the view and config
91
+ * the DraggerView with these params.
92
+ */
93
93
@ Override protected void onFinishInflate () {
94
94
super .onFinishInflate ();
95
95
if (!isInEditMode ()) {
@@ -100,17 +100,28 @@ public DraggerView(Context context, AttributeSet attrs, int defStyle) {
100
100
}
101
101
}
102
102
103
+ /**
104
+ * Add the spring listener when the view is attached.
105
+ */
103
106
@ Override protected void onAttachedToWindow () {
104
107
super .onAttachedToWindow ();
105
108
getSpring ().addListener (springListener );
106
109
}
107
110
108
- @ Override
109
- protected void onDetachedFromWindow () {
110
- super .onDetachedFromWindow ();
111
+ /**
112
+ * Remove the spring listener when the view is detached.
113
+ */
114
+ @ Override protected void onDetachedFromWindow () {
111
115
getSpring ().removeListener (springListener );
116
+ super .onDetachedFromWindow ();
112
117
}
113
118
119
+ /**
120
+ * Configure the width and height of the DraggerView.
121
+ *
122
+ * @param widthMeasureSpec Spec value of width, not represent the real width.
123
+ * @param heightMeasureSpec Spec value of height, not represent the real height.
124
+ */
114
125
@ Override protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
115
126
super .onMeasure (widthMeasureSpec , heightMeasureSpec );
116
127
int measureWidth = MeasureSpec .makeMeasureSpec (
@@ -125,12 +136,27 @@ protected void onDetachedFromWindow() {
125
136
126
137
}
127
138
139
+ /**
140
+ * Updates the view size if needed.
141
+ * @param width The new width size.
142
+ * @param height The new height size.
143
+ * @param oldWidth The old width size, useful the calculate the diff.
144
+ * @param oldHeight The old height size, useful the calculate the diff.
145
+ */
128
146
@ Override protected void onSizeChanged (int width , int height , int oldWidth , int oldHeight ) {
129
147
super .onSizeChanged (width , height , oldWidth , oldHeight );
130
148
setVerticalDragRange (height );
131
149
setHorizontalDragRange (width );
132
150
}
133
151
152
+ /**
153
+ * Detect the type of motion event (like touch)
154
+ * at the DraggerView, this can be a simple
155
+ * detector of the touch, not the listener ifself.
156
+ *
157
+ * @param ev Event of MotionEvent
158
+ * @return View is touched
159
+ */
134
160
@ Override public boolean onInterceptTouchEvent (MotionEvent ev ) {
135
161
if (!isEnabled () || !canSlide ()) {
136
162
return false ;
@@ -152,6 +178,15 @@ protected void onDetachedFromWindow() {
152
178
}
153
179
}
154
180
181
+ /**
182
+ * Handle the touch event intercepted from onInterceptTouchEvent
183
+ * method, this method valid if the touch listener
184
+ * is a valid pointer(like fingers) or the touch
185
+ * is inside of the DraggerView.
186
+ *
187
+ * @param ev MotionEvent instance, can be used to detect the type of touch.
188
+ * @return Touched area is a valid position.
189
+ */
155
190
@ Override public boolean onTouchEvent (MotionEvent ev ) {
156
191
int actionMasked = MotionEventCompat .getActionMasked (ev );
157
192
if ((actionMasked & MotionEventCompat .ACTION_MASK ) == MotionEvent .ACTION_DOWN ) {
@@ -165,6 +200,11 @@ protected void onDetachedFromWindow() {
165
200
|| isViewHit (shadowView , (int ) ev .getX (), (int ) ev .getY ());
166
201
}
167
202
203
+ /**
204
+ * This method is needed to calculate the auto scroll
205
+ * when the user slide the view to the max limit, this
206
+ * starts a animation to finish the view.
207
+ */
168
208
@ Override public void computeScroll () {
169
209
if (!isInEditMode () && dragHelper .continueSettling (true )) {
170
210
ViewCompat .postInvalidateOnAnimation (this );
@@ -179,10 +219,6 @@ public void setRunAnimationOnFinishInflate(boolean runAnimationOnFinishInflate)
179
219
this .runAnimationOnFinishInflate = runAnimationOnFinishInflate ;
180
220
}
181
221
182
- public boolean getCanAnimate () {
183
- return !animationFinish ;
184
- }
185
-
186
222
private float getVerticalDragRange () {
187
223
return verticalDragRange ;
188
224
}
@@ -243,8 +279,8 @@ private void initializeAttributes(AttributeSet attrs) {
243
279
}
244
280
245
281
private void configDragViewHelper () {
246
- dragHelperCallback = new DraggerHelperCallback (this , dragView , draggerListener );
247
- dragHelper = ViewDragHelper . create (this , SENSITIVITY , dragHelperCallback );
282
+ dragHelper = ViewDragHelper . create (this , SENSITIVITY ,
283
+ new DraggerHelperCallback (this , dragView , draggerListener ) );
248
284
}
249
285
250
286
public void setDraggerCallback (DraggerCallback draggerCallback ) {
@@ -417,9 +453,7 @@ private void finish() {
417
453
activity .overridePendingTransition (0 , android .R .anim .fade_out );
418
454
activity .finish ();
419
455
}
420
- activity = null ;
421
456
}
422
- context = null ;
423
457
}
424
458
425
459
public void setAnimationDuration (int baseSettleDuration , int maxSettleDuration ) {
@@ -435,23 +469,19 @@ public void setAnimationDuration(int miliseconds, float multipler) {
435
469
private SpringListener springListener = new SpringListener () {
436
470
@ Override public void onSpringUpdate (Spring spring ) {
437
471
438
- val = spring .getCurrentValue ();
472
+ double val = spring .getCurrentValue ();
439
473
switch (dragPosition ) {
440
474
case LEFT :
441
- progress = (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , -dragView .getWidth ());
442
- ViewCompat .setTranslationX (dragView , progress );
475
+ ViewCompat .setTranslationX (dragView , (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , -dragView .getWidth ()));
443
476
break ;
444
477
case RIGHT :
445
- progress = (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , dragView .getWidth ());
446
- ViewCompat .setTranslationX (dragView , progress );
478
+ ViewCompat .setTranslationX (dragView , (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , dragView .getWidth ()));
447
479
break ;
448
480
case TOP :
449
- progress = (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , dragView .getHeight ());
450
- ViewCompat .setTranslationY (dragView , progress );
481
+ ViewCompat .setTranslationY (dragView , (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , dragView .getHeight ()));
451
482
break ;
452
483
case BOTTOM :
453
- progress = (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , -dragView .getHeight ());
454
- ViewCompat .setTranslationY (dragView , progress );
484
+ ViewCompat .setTranslationY (dragView , (float ) SpringUtil .mapValueFromRangeToRange (val , 0 , 1 , 0 , -dragView .getHeight ()));
455
485
break ;
456
486
default :
457
487
break ;
0 commit comments