From 3b15bb7994e2e25d314369f694bc4397aa2a4772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sun, 28 Aug 2016 15:23:00 +0200 Subject: [PATCH] Added transition on swipe right. --- .../urbanexplorer/MainActivity.java | 143 +++++++++++++++++- app/src/main/res/layout/activity_main.xml | 16 +- 2 files changed, 144 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 24f15c1..6e81369 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -1,10 +1,16 @@ package pl.tpolgrabia.urbanexplorer; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import android.support.v4.view.GestureDetectorCompat; import android.support.v7.app.ActionBarActivity; +import android.util.Log; +import android.view.GestureDetector; import android.view.MenuItem; +import android.view.MotionEvent; +import android.widget.LinearLayout; import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; @@ -12,13 +18,28 @@ import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import pl.tpolgrabia.urbanexplorer.dto.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; import pl.tpolgrabia.urbanexplorer.utils.ImageLoaderUtils; -public class MainActivity extends ActionBarActivity { +public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener { private static final String CLASS_TAG = MainActivity.class.getSimpleName(); private static final String PHOTO_BACKSTACK = "PHOTO_BACKSTACK"; + private static final float SWIPE_VELOCITY_THRESHOLD = 20; + private static final int HOME_FRAGMENT_ID = 0; + private static final int WIKI_FRAGMENT_ID = 1; + private static final double MAX_FRAGMENT_ID = WIKI_FRAGMENT_ID; + private static final double MIN_FRAGMENT_ID = HOME_FRAGMENT_ID; public static DisplayImageOptions options; + private GestureDetectorCompat gestureDetector; + private float SWIPE_THRESHOLD = 50; + private int currentFragmentId = 0; + + @Override + public boolean onTouchEvent(MotionEvent event) { + gestureDetector.onTouchEvent(event); + return super.onTouchEvent(event); + } @Override protected void onCreate(Bundle savedInstanceState) { @@ -45,6 +66,9 @@ public class MainActivity extends ActionBarActivity { .add(R.id.fragments, new HomeFragment()) .commit(); + LinearLayout locations = (LinearLayout) findViewById(R.id.locations); + // locations.setOnTouchListener(new OnSwipeTouchListener); + gestureDetector = new GestureDetectorCompat(this, this); } @Override @@ -76,4 +100,121 @@ public class MainActivity extends ActionBarActivity { ctx.commit(); } + + @Override + public boolean onDown(MotionEvent e) { + return false; + } + + @Override + public void onShowPress(MotionEvent e) { + + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + return false; + } + + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + return false; + } + + @Override + public void onLongPress(MotionEvent e) { + + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + + float diffx = e2.getX() - e1.getX(); + float diffy = e2.getY() - e1.getY(); + Log.d(CLASS_TAG, "Flinging... diffx: " + diffx + " diffy" + diffy + + ", velocityx: " + velocityX + ", velocityY: " + velocityY); + + if (Math.abs(diffx) > Math.abs(diffy)) { + // horizontal moves + if (Math.abs(diffx) < SWIPE_THRESHOLD) { + return true; + } + + if (Math.abs(velocityX) < SWIPE_VELOCITY_THRESHOLD) { + return true; + } + + if (diffx > 0) { + // swipe right + swipeRight(); + } else { + // swipe left + swipeLeft(); + } + + } else { + // vertical moves + + if (Math.abs(diffy) < SWIPE_THRESHOLD) { + return true; + } + + if (Math.abs(velocityY) < SWIPE_VELOCITY_THRESHOLD) { + return true; + } + + if (diffy > 0) { + // swipe down + swipeDown(); + } else { + // swipe up + swipeUp(); + } + } + + return true; + } + + private void swipeDown() { + + } + + private void swipeUp() { + + } + + private void swipeLeft() { + currentFragmentId = (int)Math.max(MIN_FRAGMENT_ID, currentFragmentId-1); + switchFragment(); + } + + private void switchFragment() { + switch (currentFragmentId) { + case HOME_FRAGMENT_ID: + // switch to home fragment + Log.d(CLASS_TAG, "Switching to home fragment"); + switchFragment(new HomeFragment()); + break; + case WIKI_FRAGMENT_ID: + // switch to wiki fragment + Log.d(CLASS_TAG, "Switching to wiki fragment"); + switchFragment(new WikiLocationsFragment()); + break; + } + + } + + private void switchFragment(Fragment fragment) { + + FragmentManager fragmentManager = getSupportFragmentManager(); + FragmentTransaction ctx = fragmentManager.beginTransaction(); + ctx.replace(R.id.fragments, fragment); + ctx.addToBackStack(null); + ctx.commit(); + } + + private void swipeRight() { + currentFragmentId = (int)Math.min(MAX_FRAGMENT_ID, currentFragmentId+1); + switchFragment(); + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b4b5f7c..2ecdcf5 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,5 @@ - - - - - - - - - - - - - - +