4242 * Created by shadow-admin on 24/10/17.
4343 */
4444
45- public class ProSwipeButton extends RelativeLayout {
45+ public class ProSwipeButton extends RelativeLayout implements View . OnTouchListener {
4646
4747 private Context context ;
4848 private View view ;
@@ -115,6 +115,7 @@ private void setAttrs(Context context, AttributeSet attrs) {
115115 public void init () {
116116 LayoutInflater inflater = LayoutInflater .from (context );
117117 view = inflater .inflate (R .layout .view_proswipebtn , this , true );
118+ setOnTouchListener (this );
118119 }
119120
120121 @ Override
@@ -136,56 +137,51 @@ protected void onFinishInflate() {
136137 gradientDrawable .setCornerRadius (btnRadius );
137138 setBackgroundColor (bgColorInt );
138139 updateBackground ();
139- setupTouchListener ();
140140 }
141141
142- private void setupTouchListener () {
143- setOnTouchListener (new OnTouchListener () {
144- @ Override
145- public boolean onTouch (View v , MotionEvent event ) {
146- switch (event .getAction ()) {
147- case MotionEvent .ACTION_DOWN :
148- return true ;
149- case MotionEvent .ACTION_MOVE :
150- // Movement logic here
151- if (event .getX () > arrowHintContainer .getWidth () / 2 &&
152- event .getX () + arrowHintContainer .getWidth () / 2 < getWidth () &&
153- (event .getX () < arrowHintContainer .getX () + arrowHintContainer .getWidth () || arrowHintContainer .getX () != 0 )) {
154- // snaps the hint to user touch, only if the touch is within hint width or if it has already been displaced
155- arrowHintContainer .setX (event .getX () - arrowHintContainer .getWidth () / 2 );
156- }
157-
158- if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () &&
159- arrowHintContainer .getX () + arrowHintContainer .getWidth () / 2 < getWidth ()) {
160- // allows the hint to go up to a max of btn container width
161- arrowHintContainer .setX (getWidth () - arrowHintContainer .getWidth ());
162- }
163-
164- if (event .getX () < arrowHintContainer .getWidth () / 2 &&
165- arrowHintContainer .getX () > 0 ) {
166- // allows the hint to go up to a min of btn container starting
167- arrowHintContainer .setX (0 );
168- }
169-
170- return true ;
171- case MotionEvent .ACTION_UP :
172- //Release logic here
173- if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () * swipeDistance ) {
174- // swipe completed, fly the hint away!
175- performSuccessfulSwipe ();
176- } else if (arrowHintContainer .getX () <= 0 ) {
177- // upon click without swipe
178- startFwdAnim ();
179- } else {
180- // swipe not completed, pull back the hint
181- animateHintBack ();
182- }
183- return true ;
142+ @ Override
143+ public boolean onTouch (View v , MotionEvent event ) {
144+ switch (event .getAction ()) {
145+ case MotionEvent .ACTION_DOWN :
146+ return true ;
147+ case MotionEvent .ACTION_MOVE :
148+ // Movement logic here
149+ if (event .getX () > arrowHintContainer .getWidth () / 2 &&
150+ event .getX () + arrowHintContainer .getWidth () / 2 < getWidth () &&
151+ (event .getX () < arrowHintContainer .getX () + arrowHintContainer .getWidth () || arrowHintContainer .getX () != 0 )) {
152+ // snaps the hint to user touch, only if the touch is within hint width or if it has already been displaced
153+ arrowHintContainer .setX (event .getX () - arrowHintContainer .getWidth () / 2 );
184154 }
185155
186- return false ;
187- }
188- });
156+ if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () &&
157+ arrowHintContainer .getX () + arrowHintContainer .getWidth () / 2 < getWidth ()) {
158+ // allows the hint to go up to a max of btn container width
159+ arrowHintContainer .setX (getWidth () - arrowHintContainer .getWidth ());
160+ }
161+
162+ if (event .getX () < arrowHintContainer .getWidth () / 2 &&
163+ arrowHintContainer .getX () > 0 ) {
164+ // allows the hint to go up to a min of btn container starting
165+ arrowHintContainer .setX (0 );
166+ }
167+
168+ return true ;
169+ case MotionEvent .ACTION_UP :
170+ //Release logic here
171+ if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () * swipeDistance ) {
172+ // swipe completed, fly the hint away!
173+ performSuccessfulSwipe ();
174+ } else if (arrowHintContainer .getX () <= 0 ) {
175+ // upon click without swipe
176+ startFwdAnim ();
177+ } else {
178+ // swipe not completed, pull back the hint
179+ animateHintBack ();
180+ }
181+ return true ;
182+ }
183+
184+ return false ;
189185 }
190186
191187 private void performSuccessfulSwipe () {
@@ -297,7 +293,7 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
297293 }
298294
299295 private void morphToRect () {
300- setupTouchListener ( );
296+ setOnTouchListener ( this );
301297 ObjectAnimator cornerAnimation =
302298 ObjectAnimator .ofFloat (gradientDrawable , "cornerRadius" , BTN_MORPHED_RADIUS , BTN_INIT_RADIUS );
303299
0 commit comments