我有一个listview并实现了onclick和onfling.problem是当我猛扑(从左向右滑动)时,listview的onclick事件也被执行了。如何解决这个问题? 如何在列表 View 中区分触摸(轻击)和猛击(滑动)?

     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.可能吗?评论ListView的OnClickListener并在任何onTouchEvent中编写相同的逻辑?但我仍然毫无疑问,onFling将调用onTouch。我对吗?

最佳答案

伪代码答案来澄清以上注释。如何调用MySimpleGestureListener的onTouch方法。

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

关于android - 如何在同一 ListView 中实现ontouch和onfling?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4184382/

10-09 03:28