diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 6e81369..4349bb7 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -10,7 +10,6 @@ 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; @@ -20,6 +19,8 @@ import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment; import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; import pl.tpolgrabia.urbanexplorer.utils.ImageLoaderUtils; +import pl.tpolgrabia.urbanexplorer.views.CustomInterceptor; +import pl.tpolgrabia.urbanexplorer.views.SwipeFrameLayout; public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener { @@ -35,12 +36,6 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O 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) { super.onCreate(savedInstanceState); @@ -66,9 +61,12 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O .add(R.id.fragments, new HomeFragment()) .commit(); - LinearLayout locations = (LinearLayout) findViewById(R.id.locations); + + + // LinearLayout locations = (LinearLayout) findViewById(R.id.locations); // locations.setOnTouchListener(new OnSwipeTouchListener); gestureDetector = new GestureDetectorCompat(this, this); + updateSwipeHandler(); } @Override @@ -184,7 +182,7 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O } private void swipeLeft() { - currentFragmentId = (int)Math.max(MIN_FRAGMENT_ID, currentFragmentId-1); + currentFragmentId = (int)Math.min(MAX_FRAGMENT_ID, currentFragmentId+1); switchFragment(); } @@ -211,10 +209,21 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O ctx.replace(R.id.fragments, fragment); ctx.addToBackStack(null); ctx.commit(); + updateSwipeHandler(); + } + + private void updateSwipeHandler() { + SwipeFrameLayout swipeFragments = (SwipeFrameLayout) findViewById(R.id.fragments); + swipeFragments.setCustomInterceptor(new CustomInterceptor() { + @Override + public void handle(MotionEvent ev) { + gestureDetector.onTouchEvent(ev); + } + }); } private void swipeRight() { - currentFragmentId = (int)Math.min(MAX_FRAGMENT_ID, currentFragmentId+1); + currentFragmentId = (int)Math.max(MIN_FRAGMENT_ID, currentFragmentId-1); switchFragment(); } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/views/CustomInterceptor.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/views/CustomInterceptor.java new file mode 100644 index 0000000..9c21ad3 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/views/CustomInterceptor.java @@ -0,0 +1,10 @@ +package pl.tpolgrabia.urbanexplorer.views; + +import android.view.MotionEvent; + +/** + * Created by tpolgrabia on 28.08.16. + */ +public interface CustomInterceptor { + void handle(MotionEvent ev); +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/views/SwipeFrameLayout.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/views/SwipeFrameLayout.java new file mode 100644 index 0000000..479508a --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/views/SwipeFrameLayout.java @@ -0,0 +1,41 @@ +package pl.tpolgrabia.urbanexplorer.views; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.widget.FrameLayout; + +/** + * Created by tpolgrabia on 28.08.16. + */ +public class SwipeFrameLayout extends FrameLayout { + private CustomInterceptor customInterceptor; + + public SwipeFrameLayout(Context context) { + super(context); + } + + public SwipeFrameLayout(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SwipeFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public SwipeFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + if (customInterceptor != null) { + customInterceptor.handle(ev); + } + return super.onInterceptTouchEvent(ev); + } + + public void setCustomInterceptor(CustomInterceptor customInterceptor) { + this.customInterceptor = customInterceptor; + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2ecdcf5..2e0322c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,5 @@ - - +