-2

I'm in the process of making an app that slides facts across the screen left and right, my issue is that I cannot find a default class to do this, instead a VERY SIMPLE action that is commonly used needs this:

public boolean onTouchEvent(MotionEvent event)
 {     
     switch(event.getAction())
     {
       case MotionEvent.ACTION_DOWN:
           x1 = event.getX();                         
   break;         
   case MotionEvent.ACTION_UP:
       x2 = event.getX();
       float deltaX = x2 - x1;
       if (Math.abs(deltaX) > MIN_DISTANCE)
       {
         Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show ();
       }
       else
       {
           // consider as something else - a screen tap for example
       }                          
   break;   
 }           
 return super.onTouchEvent(event);       
 }

or something equally as difficult and foreign for a BASIC MOVEMENT.

Is there an easier way? I find unacceptable for an SDK of such a massive OS to not have a default class to handle this, especially since there's an 'ACTION_' for just about everything except left and right.

420blazeit
  • 61
  • 1
  • 8

1 Answers1

3

Many high-level use cases can be handled by ViewPager, HorizontalScrollView, or RecyclerView. There is also the deprecated Gallery widget.

Many lower-level use cases can be handled by GestureDetector, such as the family of solutions on this SO question. However, you have to define exactly what constitutes "BASIC MOVEMENT" (to use your shouty term).

There is also the old GestureLibrary stuff, which I haven't seen used in ages, so I don't know if it is still practical.

There are also many libraries that offer swipe options for different scenarios (e.g., swipe-to-dismiss in list rows).

In the future, you might consider providing a more concrete scenario (and less whining), to get more specific recommendations. Your description of "slides facts across the screen left and right" is rather vague.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have a textview in the centre of the screen and I want it to change based on whether they swipe left or right (to go forward or back a fact). – 420blazeit Mar 17 '16 at 23:27
  • @420blazeit: Usually, for swipe gestures, you want the thing being swiped to move, to show visual feedback to the user. You do not appear to want that, so the high-level solutions that I listed will not work for you. One of the libraries might handle that, though I am not aware of any that would. The `GestureDetector` options would be your best bet. Or, reconsider your UI, to use patterns that the user might already be used to and that fit existing supported scenarios. – CommonsWare Mar 17 '16 at 23:30
  • @420blazeit: Now, if I misinterpreted your "have a textview in the centre of the screen" part, and you mean that you want it to start in the center, but then follow the finger and be swiped off... then `ViewPager` is what you want. It handles all the gesture stuff for you; you just tell it how many pages there are and what the page contents are (`Views` or `Fragments`). – CommonsWare Mar 17 '16 at 23:39
  • What I was originally looking to do was make it so swiping left or right acted as an action listener and then the textview text changes instantly when they swipe. How would I do this? Similar to this: `swipe.OnSwipeLeft(){` `textview.setText("You swiped Left!");` `}` – 420blazeit Mar 18 '16 at 05:20
  • @420blazeit: Again, that's not especially typical of modern touchscreen UIs, which is why you are going to have to deal with lower-level stuff yourself. `GestureDetector`, using the stuff from [the question that I linked to](http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures), would be your best bet. – CommonsWare Mar 18 '16 at 11:22