Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>onFling</code> is a method available to members of the <code>SimpleOnGestureListener</code> <a href="http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html" rel="nofollow">class</a>. To override this method you either need to (1) create a class that extends <code>SimpleOnGestureListener</code> or (2) implement a gesture listener within your existing class. Then you can override <code>onFling</code>. </p> <p><strong>Update</strong></p> <p>Sorry for the delay. Here's my implementation. In the activity that you want to use the <code>onFling</code> you need to set up a <code>GestureDetector</code> that uses your custom <code>onFling</code> listener. You can see I'm using a <code>FlingGestureListener</code> the code for which is below. I'm listening for touch on a specific image, peopleTab, but you should be able to substitute any view. </p> <pre><code> final GestureDetector gdt = new GestureDetector(getActivity(), FlingGestureListener.getInstance(tabSwipeListener)); final ImageView peopleTab = (ImageView)getActivity().findViewById(R.id.peopleFlag); peopleTab.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gdt.onTouchEvent(event); } }); </code></pre> <p>Then for FlingGestureListener I use a separate class that extends <code>SimpleOnGestureListener</code> so I can choose to only implement <code>onFling</code></p> <pre><code>public class FlingGestureListener extends SimpleOnGestureListener { private static final int SWIPE_MIN_DISTANCE = 80; private static final int SWIPE_THRESHOLD_VELOCITY = 50; private static final int PEOPLE_FRAGMENT = 0; private static final int PLACES_FRAGMENT = 2; private OnTabSwipedListener tabSwipeListener; private FlingGestureListener(OnTabSwipedListener tabSwipeListener){ this.tabSwipeListener = tabSwipeListener; } /** * Static factory * @param listener called when tab swiped * @return */ public static FlingGestureListener getInstance(OnTabSwipedListener listener){ return new FlingGestureListener(listener); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ if(e1.getX() - e2.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY){ Log.d("SWIPE", "right to left"); tabSwipeListener.onTabSwipe(true, PLACES_FRAGMENT); //sent int to fragment container to switch pager to that view return true; //Right to left } else if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY){ Log.d("SWIPE", "left to right"); tabSwipeListener.onTabSwipe(true, PEOPLE_FRAGMENT); return true; //Left to right } //This will test for up and down movement. if(e1.getY() - e2.getY() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY){ return false; //Bottom to top } else if (e2.getY() - e1.getY() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY){ return false; //Top to bottom } return false; } </code></pre> <p>}</p> <p>I use a callback from this class to indicate when a swipe has occurred. The booleans that are returned are just to indicate whether the whole touch event has been handled. Hopefully this helps.</p> <p><strong>UPDATE 2</strong></p> <p>With the interfaces I am using something specific to my set up.</p> <pre><code>tabSwipeListener.onTabSwipe(true, PLACES_FRAGMENT); </code></pre> <p>this indicates first that a swipe has occurred and in what direction. I use this in a 3 fragment <code>ViewPager</code> set up. From the middle page swiping right goes to a list of places and swiping left goes to a list of people. The PLACES_FRAGMENT constant for example is an integer that refers to the fragment id within the <code>ViewPager</code>. Passing back that integer I can then tell the <code>ViewPager</code> to change to display the fragment at position 2 which effectivtly moves the page. </p> <p>I don't know how to give you a code example because I dont know that this is what you need to do. If you just need to detect a swipe then your interface need only pass back a boolean. If true you will know that the <code>onFling</code> method has been called and whatever threshold criteria you set have been met. You then need to handle that response appropritely within the context of your app. I don't think there is any more advice I can give you. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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