Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: How to combine Swipe gesture with Longpress in a item on listView
    primarykey
    data
    text
    <p>I have a listview which I personalized and I added <code>setOnItemLongClickListener()</code> which worked well. Then I decided to implement a gesture <code>listView.setOnTouchListener(new OnSwipeTouchListener())</code> which also works well. I copied the <code>OnSwipetouchListener</code> class from another post.</p> <p>The thing is that when I added the swipe listener the longPress didn't work anymore. I guess it's because the swipe listener takes the longpress actions for itself and doesn't allow longPress to do anything.</p> <p>What I want to do:</p> <p>The swipe listener gets everything below 2 seconds, after that everything goes to longpress. So I can still change the listview content by a swipe gesture and I can also create functions for each listitem.</p> <p>My code:</p> <pre><code>public class OnSwipeTouchListener implements OnTouchListener { private final GestureDetector gestureDetector = new GestureDetector(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) &gt; Math.abs(diffY)) { if (Math.abs(diffX) &gt; SWIPE_THRESHOLD &amp;&amp; Math.abs(velocityX) &gt; SWIPE_VELOCITY_THRESHOLD) { if (diffX &gt; 0) { onSwipeRight(); } else { onSwipeLeft(); } } } else { if (Math.abs(diffY) &gt; SWIPE_THRESHOLD &amp;&amp; Math.abs(velocityY) &gt; SWIPE_VELOCITY_THRESHOLD) { if (diffY &gt; 0) { onSwipeBottom(); } else { onSwipeTop(); } } } } catch (Exception exception) { exception.printStackTrace(); } return result; } ...methods... } </code></pre>
    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.
 

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