Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

android - how to implement both ontouch and also onfling in a same listview?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }       
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...