Просмотр исходного кода

Improved main menu behaviour.
Added some missing strings.

seavenois 10 лет назад
Родитель
Сommit
63eb332386

+ 47 - 37
app/src/main/java/com/ivalentin/margolariak/MainLayout.java

@@ -4,15 +4,17 @@ import android.annotation.SuppressLint;
 import android.content.Context;
 import android.os.Handler;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.animation.Interpolator;
 import android.widget.LinearLayout;
+import android.widget.ScrollView;
 import android.widget.Scroller;
 
 /**
  * Extension of LinearLayout to be used in the app. Contains the slider menu.
- * 
+ *
  * @author Iñigo Valentin
  *
  */
@@ -20,13 +22,13 @@ public class MainLayout extends LinearLayout {
 
 	//The width of the view
 	private int mainLayoutWidth;
-	
+
 	//The menu view
 	private View menu;
-	
+
 	//The content view
 	private View content;
-	
+
 	//The margin of the menu
 	private static int menuRightMargin = 25;
 
@@ -39,15 +41,17 @@ public class MainLayout extends LinearLayout {
 
 	private int contentXOffset;
 	private MenuState currentMenuState = MenuState.HIDDEN;
-	private final Scroller menuScroller = new Scroller(this.getContext(), new EaseInInterpolator());
-	private final Runnable menuRunnable = new MenuRunnable();
-	private final Handler menuHandler = new Handler();
+	private Scroller menuScroller = new Scroller(this.getContext(), new EaseInInterpolator());
+	private Runnable menuRunnable = new MenuRunnable();
+	private Handler menuHandler = new Handler();
 	private int prevX = 0;
 	private boolean isDragging = false;
+	private int lastDiffX = 0;
+	private int initialX = 0;
 
 	/**
 	 * Constructor.
-	 * 
+	 *
 	 * @param context Context of the app.
 	 * @param attrs Atributes.
 	 */
@@ -57,7 +61,7 @@ public class MainLayout extends LinearLayout {
 
 	/**
 	 * Constructor.
-	 * 
+	 *
 	 * @param context Context of the app.
 	 */
 	public MainLayout(Context context) {
@@ -71,7 +75,7 @@ public class MainLayout extends LinearLayout {
 		mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
 		menuRightMargin = mainLayoutWidth * 25 / 100;
 	}
-	
+
 	@SuppressLint("ClickableViewAccessibility") //I don't need it.
 	@Override
 	protected void onAttachedToWindow() {
@@ -105,13 +109,13 @@ public class MainLayout extends LinearLayout {
 	}
 
 	/**
-	 * Changes the menu state. 
+	 * Changes the menu state.
 	 * Closes it if it's open. Opens it if it's closed.
 	 */
 	public void toggleMenu() {
 		if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
 			return;
-		
+
 		switch (currentMenuState) {
 			case HIDDEN:
 				currentMenuState = MenuState.SHOWING;
@@ -128,33 +132,33 @@ public class MainLayout extends LinearLayout {
 		menuHandler.postDelayed(menuRunnable, GM.MENU_QUERY_INTERVAL);
 		this.invalidate();
 	}
-	
+
 	/**
 	 * Handles the animation of the sliding menu.
-	 * 
+	 *
 	 * @author seavenois
-	 * 
+	 *
 	 * @see Runnable
 	 *
 	 */
-	private class MenuRunnable implements Runnable {
+	protected class MenuRunnable implements Runnable {
 		@Override
 		public void run() {
 			boolean isScrolling = menuScroller.computeScrollOffset();
 			adjustContentPosition(isScrolling);
 		}
 	}
-		
+
 	/**
-	 * Moves the content view when the menu is slided. 
-	 * 
+	 * Moves the content view when the menu is slided.
+	 *
 	 * @param isScrolling Indicates if the menu is being slided.
 	 */
 	private void adjustContentPosition(boolean isScrolling) {
 		int scrollerXOffset = menuScroller.getCurrX();
-		
+
 		content.offsetLeftAndRight(scrollerXOffset - contentXOffset);
-		
+
 		contentXOffset = scrollerXOffset;
 		this.invalidate();
 		if (isScrolling)
@@ -162,9 +166,9 @@ public class MainLayout extends LinearLayout {
 		else
 			this.onMenuSlidingComplete();
 	}
-	
+
 	/**
-	 * Called when the menu has been slided completely. 
+	 * Called when the menu has been slided completely.
 	 * Fixes the position.
 	 */
 	private void onMenuSlidingComplete() {
@@ -178,29 +182,29 @@ public class MainLayout extends LinearLayout {
 				break;
 		}
 	}
-	
+
 	/**
 	 * Defines the menu animation frame rate.
-	 * 
+	 *
 	 * @author Iñigo Valentin
 	 *
 	 */
-	private class EaseInInterpolator implements Interpolator {
+	protected class EaseInInterpolator implements Interpolator {
 		@Override
 		public float getInterpolation(float t) {
 			return (float) Math.pow(t - 1, 5) + 1;
 		}
-	
+
 	}
 
 	/**
-	 * Register touch events. 
+	 * Register touch events.
 	 * Used to show and hide the menu.
-	 * 
+	 *
 	 * @param event The MotionEvent triggering it.
 	 * @return True if the menu is fully opened or closed and a touch event is happening
 	 */
-	private boolean onContentTouch(MotionEvent event) {
+	public boolean onContentTouch(MotionEvent event) {
 		if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
 			return false;
 		int curX = (int) event.getRawX();
@@ -209,9 +213,13 @@ public class MainLayout extends LinearLayout {
 		switch (event.getAction()) {
 			case MotionEvent.ACTION_DOWN:
 				prevX = curX;
+				initialX = curX;
 				return true;
-			
+
 			case MotionEvent.ACTION_MOVE:
+				//int width = this.getWidth();
+				//if (event.getX() <  (float) width / 4){
+
 				if (!isDragging) {
 
 					isDragging = true;
@@ -229,15 +237,16 @@ public class MainLayout extends LinearLayout {
 				this.invalidate();
 
 				prevX = curX;
-
+				lastDiffX = diffX;
+				//}
 				return true;
-			
+
 			case MotionEvent.ACTION_UP:
-				if (currentMenuState == MenuState.HIDDEN) {
+				if (initialX != curX && currentMenuState == MenuState.HIDDEN) {
 					currentMenuState = MenuState.SHOWING;
 					menuScroller.startScroll(contentXOffset, 0,	menu.getLayoutParams().width - contentXOffset, 0, GM.MENU_SLIDING_DURATION);
 				}
-				else if (currentMenuState == MenuState.SHOWN) {
+				else if (initialX != curX && currentMenuState == MenuState.SHOWN) {
 
 					currentMenuState = MenuState.HIDING;
 					menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0, GM.MENU_SLIDING_DURATION);
@@ -246,14 +255,15 @@ public class MainLayout extends LinearLayout {
 				this.invalidate();
 				isDragging = false;
 				prevX = 0;
+				lastDiffX = 0;
 				return true;
-				
+
 			default:
 				break;
 		}
 
 
-		
+
 		return false;
 	}
 }

+ 4 - 0
app/src/main/res/values/strings.xml

@@ -109,6 +109,10 @@
 	<string name="comment_toast_text">Introduce un comentario</string>
 	<string name="comment_toast_user">Introduce un nombre</string>
 	<string name="comment_your_comment">Tu comentario</string>
+	<plurals name="comment_comments">
+		<item quantity="one">%1$d comentario</item>
+		<item quantity="other">%1$d comentarios</item>
+	</plurals>
 	<string name="dialog_sync_text_0">Contactando con Don Margolo&#8230;</string>
 	<string name="dialog_sync_text_1">Arrancando la furgo&#8230;</string>
 	<string name="dialog_sync_text_2">Preparando kalimotxos&#8230;</string>