Added transition on swipe right.

master
Tomasz Półgrabia 2016-08-28 15:23:00 +02:00
parent 58411cf77e
commit 3b15bb7994
2 changed files with 144 additions and 15 deletions

View File

@ -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();
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<FrameLayout
android:id="@+id/fragments"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
@ -11,17 +11,5 @@
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="pl.tpolgrabia.urbanexplorer.MainActivity" android:orientation="horizontal">
<!--<Toolbar android:id="@+id/navbar"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content">-->
<!--</Toolbar>-->
<!--<fragment-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:name="pl.tpolgrabia.urbanexplorer.fragments.HomeFragment"-->
<!--android:id="@+id/home_frag"-->
<!--tools:layout="@layout/fragment_home"/>-->
</LinearLayout>
</FrameLayout>