I'm trying to use gesture for change the Fragment in my MainActivity.
I use this code.
Main Activity :
public class MainActivity extends FragmentActivity {
/* ... */
HomeGestureDetector homeGestureDetector = new HomeGestureDetector(MainActivity.this){
public void onSwipeRight() {
menuListener((selectedItem-1 == -1)? 4 : selectedItem-1) ;
}
public void onSwipeLeft() {
menuListener((selectedItem + 1) % 5);
}
};
center.setOnTouchListener(new GestureManager(new GestureDetector(MainActivity.this, homeGestureDetector)));
}
GestureManager:
public class GestureManager implements OnTouchListener {
private final GestureDetector gestureDetector;
public GestureManager(GestureDetector gd) {
gestureDetector = gd;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
Finally HomeGestureDetector is the class that extends SimpleOnGestureListener but like you can see the implementation of the method onSwipeRight() onSwipeLeft() is delegate in the MainActivity.
Initially this code works fine but when I fill the layout with other elements, seems that the Gesture can not be intercepted cause the listener is on container layout and when I click obviously I touch the conteined element, Not the container.
How can I implement this feature?
Consider revising your implementation by using a ViewPager.
A ViewPager is a ...
Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
(from the docs)
Interesting/Required reading to get (I'd post code but developer.android.com has got EVERYTHING you need here) :
https://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
https://developer.android.com/training/implementing-navigation/lateral.html#swipe-tabs
MainActivity#menuListener
methodhttps://developer.android.com/design/patterns/swipe-views.html
hth!