Reputation: 4201
I am trying to implement swiping between pages on my Application. I currently have 5 XML layout files which I access all from one Activity. There are five buttons on the bottom which represent each layout and when pressed switch to the corresponding XML layout.
I have tried to implement swiping using OnGestureListener
, but it does not work as I would like it to. I need to swipe the Title
of the view for the swiping to work. It does not work when I swipe across any of the Views shown in the layout.
I began looking and found the GestureOverlayView
, which seems to be what I want. However, all of implementations I've found give me problems. Tutorial, Similar SO question.
This is the code that I have so far:
public class Monitor extends Activity implements android.view.GestureDetector.OnGestureListener, OnGesturePerformedListener {
private GestureDetector gestureScanner;
private GestureOverlayView gestures;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
private GestureLibrary gestureLib;
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
gestures = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestures.addView(inflate);
gestures.addOnGesturePerformedListener(this);
gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures); // Error here
if (!gestureLib.load()) {
finish();
}
setContentView(gestures);
}
I get an error on the line gestureLib = GestureLibraries.fromRawResource();
. The error is raw cannot be resolved or is not a field.
. There is no raw
folder in my /project-name/res/
directory. Any ideas? Is there another way to do this?
This is how I first implemented the swiping capability, which only worked across the title bar.
public class Monitor extends Activity implements android.view.GestureDetector.OnGestureListener {
private GestureDetector gestureScanner;
private static final int SWIPE_MIN_DISTANCE = 50;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
gestureScanner = new GestureDetector(this);
setContentView( R.layout.monitor );
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i( "TouchEvent", "Here" );
return gestureScanner.onTouchEvent(event);
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
) {
Log.i( "OnFling", "Right" );
//ur code goes here.
if( getTitle().equals( getString( R.string.main_analogs ) ) ) {
setContentView( R.layout.monitor );
setTitle( R.string.main_monitor );
buildButtons();
if( hasRead ) {
updateMonitorForm();
}
}
}
/* on scroll to the previous page */
else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
) {
Log.i( "OnFling", "Left" );
//Ur code goes here
if( getTitle().equals( getString( R.string.main_monitor ) ) ) {
setContentView( R.layout.analogs );
setTitle( R.string.main_analogs );
buildButtons();
if( hasRead ) {
updateMonitorForm();
}
}
}
return false;
}
Upvotes: 1
Views: 1738
Reputation: 87064
I am trying to implement swiping between pages on my Application. I currently have 5 XML layout files which I access all from one Activity. There are five buttons on the bottom which represent each layout and when pressed switch to the corresponding XML layout.
This will be an ideal scenario for the ViewPager
widget(unless you forgot to add other important details). You can manually swipe the ViewPager
(so you don't need to handle the touch events on your own) and you could also set the current page of the ViewPager
with the setCurrentItem
method(the Buttons
part).
I've made my little example based on your scenario. The sample it's pretty self explanatory and you can find it here.
Upvotes: 2