16

i have a listview and implemented onclick and also onfling.problem is when i do fling(swipe left to right), onclick event of listview is also getting executed.How to overCome this problem? how to differentiate touch(tap) and fling(swipe) in listview?

     listClickListener = new OnItemClickListener() {

           public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
            //Job of Onclick Listener     
           }
      };
       mContactList.setOnItemClickListener(listClickListener); 
        mContactList.setAdapter(adapter);
        // Gesture detection 
        gestureDetector = new GestureDetector(new MyGestureDetector(prosNos)); 
         gestureListener = new View.OnTouchListener() { 
             public boolean onTouch(View v, MotionEvent event) { 
                 if (gestureDetector.onTouchEvent(event)) { 
                     return true; 
                 } 
                 return false; 
             } 
         }; 

         mContactList.setOnTouchListener(gestureListener); 

        }

     public class MyGestureDetector extends SimpleOnGestureListener { 

        @Override 
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
           // My fling event
           return false;
        }
    }

P.S. Is it possible? to comment the OnClickListener of ListView and writing the same logic in any onTouchEvent? but still I have no doubt that onFling will call onTouch. Am I right?

edwoollard
  • 12,245
  • 6
  • 43
  • 74
Ads
  • 6,681
  • 12
  • 47
  • 72
  • 1
    Try these for more information, they helped me implement what your looking for. http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding-touch-events/1775 http://android-journey.blogspot.com/2010/01/android-gestures.html – Emile Nov 15 '10 at 12:47
  • @Emile: thank u for the post.when i override the ontouch function within MyGestureDetector class it is not working. is it possible to do that? or else i have to have in touchlistener? – Ads Nov 16 '10 at 06:26
  • 1
    yep i found that out also. Yep, gets a bit confusing doesn't it. A lot of the examples will have you add code to the onTouch method, but you'll often want this to relate to the MyGestureDetector classes methods. – Emile Nov 16 '10 at 09:29
  • I'll post an example. Can't guarantee it will work as its more psuedo code. – Emile Nov 16 '10 at 09:31

2 Answers2

20

Pseudo code answer to clarify the above comments. How to have the MySimpleGestureListener's onTouch method called.

public class GestureExample extends Activity {

    protected MyGestureListener myGestureListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        myGestureListener = new MyGestureListener(this);
        // or if you have already created a Gesture Detector.
        //   myGestureListener = new MyGestureListener(this, getExistingGestureDetector());


        // Example of setting listener. The onTouchEvent will now be called on your listener
        ((ListView)findViewById(R.id.ListView)).setOnTouchListener(myGestureListener);


    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // or implement in activity or component. When your not assigning to a child component.
        return myGestureListener.getDetector().onTouchEvent(event); 
    }


    class MyGestureListener extends SimpleOnGestureListener implements OnTouchListener
    {
        Context context;
        GestureDetector gDetector;

        public MyGestureListener()
        {
            super();
        }

        public MyGestureListener(Context context) {
            this(context, null);
        }

        public MyGestureListener(Context context, GestureDetector gDetector) {

            if(gDetector == null)
                gDetector = new GestureDetector(context, this);

            this.context = context;
            this.gDetector = gDetector;
        }


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {

            return super.onSingleTapConfirmed(e);
        }





        public boolean onTouch(View v, MotionEvent event) {

            // Within the MyGestureListener class you can now manage the event.getAction() codes.

            // Note that we are now calling the gesture Detectors onTouchEvent. And given we've set this class as the GestureDetectors listener 
            // the onFling, onSingleTap etc methods will be executed.
            return gDetector.onTouchEvent(event);
        }


        public GestureDetector getDetector()
        {
            return gDetector;
        }       
    }
}
Emile
  • 11,451
  • 5
  • 50
  • 63
  • 2
    Note, That whilst this clarifies the onTouch events, it doesn't strictly answer the issue of the tap event being fired on the list view. I think the issue may lie in your onFling code. You need to ensure that you return true when you happy you've detected a sufficient fling for you needs. That way you capture the event and stop other methods/detections from being interpreted. – Emile Nov 16 '10 at 09:55
  • You found the exact problem of mine...Very sorry i forgot to return true when the event is executed – Ads Nov 17 '10 at 06:46
  • in emulator onfling method is working fine.But in device it is not working? i hope gesture is not recognized by the device. how to make that work? – Ads Nov 18 '10 at 01:54
  • I just wanted to say that this worked for me only when I returned true in onDown(). – kkudi Mar 16 '11 at 15:09
0

Catch the Multi Click Event

I Solve this problem by this way, It is pretty Simple!

Object.setOnTouchListener( new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                int action = event.getActionMasked();

                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        // button press
                        initialX = event.getX();
                        initialY = event.getY();
                        break;

                    case MotionEvent.ACTION_UP:

                        //button release
                        float finalX = event.getX();
                        float finalY = event.getY();

                       if (initialX > finalX)
                       {
                           //Right to Left swipe performed
                       }
                       if (initialX < finalX)
                       {
                           //Left to Right swipe performed
                       }
                       if (initialY < finalY) {
                           //Log.d( TAG, "Up to Down swipe performed" );
                       }
                       if (initialY > finalY) {
                           //Log.d( TAG, "Down to Up swipe performed" );
                       }
                       break;
                   case MotionEvent.ACTION_CANCEL:
                       Log.d( TAG, "Action was CANCEL" );
                       break;
                   case MotionEvent.ACTION_OUTSIDE:
                       Log.d( TAG, "Movement occurred outside bounds of current screen element" );
                       break;
                   case MotionEvent.ACTION_MOVE:
                       Toast.makeText(getApplicationContext(), "Action was MOVE",Toast.LENGTH_SHORT ).show();
                       break;


                }
                return true;
            }
        } );
Sohaib Aslam
  • 1,245
  • 17
  • 27