Browse Source

Improved swipe gestures in the profile manager

Alexander Brakowski 10 years ago
parent
commit
9241a9d209

+ 15 - 43
src/de/tudarmstadt/informatik/hostage/ui2/adapter/ProfileManagerListAdapter.java

@@ -17,6 +17,7 @@ import android.widget.ViewSwitcher;
 import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.R;
+import de.tudarmstadt.informatik.hostage.ui2.listeners.OnSwipeTouchListener;
 import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
 
 /**
@@ -81,50 +82,21 @@ public class ProfileManagerListAdapter extends ArrayAdapter<ProfileListItem> {
 		    holder = (ViewHolder) rowView.getTag();
 	    }
 
-	    holder.switcher.setOnTouchListener(new View.OnTouchListener(){
-		    @Override
-		    public boolean onTouch(View v, MotionEvent event) {
-			    float currentX = event.getX();
-
-			    ViewHolder holder = (ViewHolder) v.getTag();
-
-			    System.out.println("_______________ MOTION! " + event.toString());
-			    switch(event.getAction()) {
-				    case  MotionEvent.ACTION_DOWN:
-					    mLastX = currentX;
-					    return true;
-				    case MotionEvent.ACTION_MOVE:
-					    return true;
-				    case MotionEvent.ACTION_UP:
-					    upX = event.getX();
-
-					    float deltaX = mLastX - upX;
-
-					    // horizontal swipe detection
-					    if (Math.abs(deltaX) > MIN_DISTANCE) {
-						    if (deltaX < 0) {
-							    // swipe left to right
-							    if(item.isBackVisible){
-								    holder.switcher.setDisplayedChild(0);
-								    item.isBackVisible = false;
-							    }
-
-							    return true;
-						    }
-						    if (deltaX > 0) {
-							    // swipe right to left
-							    if(!item.isBackVisible){
-								    holder.switcher.setDisplayedChild(1);
-								    item.isBackVisible = true;
-							    }
-
-							    return true;
-						    }
-					    }
-
-					    break;
+	    final ViewSwitcher switcher = holder.switcher;
+
+	    holder.switcher.setOnTouchListener(new OnSwipeTouchListener(){
+		    public void onSwipeRight() {
+			    if(item.isBackVisible){
+				    switcher.setDisplayedChild(0);
+				    item.isBackVisible = false;
+			    }
+		    }
+
+		    public void onSwipeLeft() {
+			    if(!item.isBackVisible){
+				    switcher.setDisplayedChild(1);
+				    item.isBackVisible = true;
 			    }
-			    return false;
 		    }
 	    });
 

+ 74 - 0
src/de/tudarmstadt/informatik/hostage/ui2/listeners/OnSwipeTouchListener.java

@@ -0,0 +1,74 @@
+package de.tudarmstadt.informatik.hostage.ui2.listeners;
+
+import android.view.GestureDetector;
+import android.view.GestureDetector.SimpleOnGestureListener;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.View.OnTouchListener;
+
+import de.tudarmstadt.informatik.hostage.ui.MainActivity;
+
+/**
+ * @author Alexander Brakowski
+ * @created 27.01.14 23:47
+ */
+public class OnSwipeTouchListener implements OnTouchListener {
+
+	private final GestureDetector gestureDetector = new GestureDetector(MainActivity.getContext(), new GestureListener());
+
+	public boolean onTouch(final View view, final MotionEvent motionEvent) {
+		return gestureDetector.onTouchEvent(motionEvent);
+	}
+
+	private final class GestureListener extends SimpleOnGestureListener {
+
+		private static final int SWIPE_THRESHOLD = 100;
+		private static final int SWIPE_VELOCITY_THRESHOLD = 100;
+
+		@Override
+		public boolean onDown(MotionEvent e) {
+			return true;
+		}
+
+		@Override
+		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
+			boolean result = false;
+			try {
+				float diffY = e2.getY() - e1.getY();
+				float diffX = e2.getX() - e1.getX();
+				if (Math.abs(diffX) > Math.abs(diffY)) {
+					if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
+						if (diffX > 0) {
+							onSwipeRight();
+						} else {
+							onSwipeLeft();
+						}
+					}
+				} else {
+					if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
+						if (diffY > 0) {
+							onSwipeBottom();
+						} else {
+							onSwipeTop();
+						}
+					}
+				}
+			} catch (Exception exception) {
+				exception.printStackTrace();
+			}
+			return result;
+		}
+	}
+
+	public void onSwipeRight() {
+	}
+
+	public void onSwipeLeft() {
+	}
+
+	public void onSwipeTop() {
+	}
+
+	public void onSwipeBottom() {
+	}
+}