I am making a app in which I have a textview(occupying most of my activity). Textview displays a large amount of text across multiple pages (pages are nothing but the large text assigned into a string array and each array element being displayed on page change trigger). I used the code from below thread to introduce right/left swipe to navigate across pages.
Android: How to handle right to left swipe gestures
Below is the exact code I am using in my all.
public class OnSwipeTouchListener extends Activity implements OnTouchListener{
private final GestureDetector gestureDetector = new GestureDetector(getBaseContext(), new GestureListener());
public boolean onTouch(final View v, final MotionEvent event) {
//super.onTouch(v, event);
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
and then this is how I use this class in my activity
myTextView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
displayPreviousPage() //code to display previous page
}
public void onSwipeLeft() {
displayNextPage() //code to display next page
}
});
Everything works fine till now. Now I wanted to enable vertical scrolling for my textview in case my text goes beyond the screen height. Hence I added the below line in my code to do the same.
myTextView.setMovementMethod(new ScrollingMovementMethod());
But that doesn’t seem to work. Any suggestions why? Is it that I have overridden onFling method in my OnSwipeTouchListener class and that setMovementMethod was also using the same(onFling method) for its implementation?