Note that there are some explanatory texts on larger screens.

plurals
  1. POSwiping between XML layouts with OnGestureListener or OnGesturePerformedListener
    text
    copied!<p>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.</p> <p>I have tried to implement swiping using <code>OnGestureListener</code>, but it does not work as I would like it to. I need to swipe the <code>Title</code> of the view for the swiping to work. It does not work when I swipe across any of the Views shown in the layout. </p> <p>I began looking and found the <code>GestureOverlayView</code>, which seems to be what I want. However, all of implementations I've found give me problems. <a href="http://www.vogella.com/articles/AndroidGestures/article.html" rel="nofollow noreferrer">Tutorial</a>, <a href="https://stackoverflow.com/questions/5434258/using-a-gesture-overlay-view-in-android">Similar SO question</a>. </p> <p>This is the code that I have so far:</p> <pre><code>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); } </code></pre> <p>I get an error on the line <code>gestureLib = GestureLibraries.fromRawResource();</code>. The error is <code>raw cannot be resolved or is not a field.</code>. There is no <code>raw</code> folder in my <code>/project-name/res/</code> directory. Any ideas? Is there another way to do this?</p> <p>This is how I first implemented the swiping capability, which only worked across the title bar. </p> <pre><code>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() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; 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() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload