10

我有以下 ListActivity:

public class ShowDayActivity extends ListActivity implements OnItemClickListener {
    private GestureDetector gestureDetector;
    private View.OnTouchListener gestureListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.day);
        registerForContextMenu(getListView());

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };

        getListView().setOnItemClickListener(this);
        getListView().setOnTouchListener(gestureListener);
    }

    @SuppressWarnings("static-access")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        ...
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        ...
        return super.onContextItemSelected(item);
    }

    Override
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        editEvent(pos);
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, 
                               float velocityX, float velocityY) {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
               return false;
            }
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.d("ICS-Calendar", "Fling left");
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Log.d("ICS-Calendar", "Fling right");
                return true;
            }

            return false;
        }
    }   
} 

listItems 上的 contextListener (长按)运行良好。今天我添加了gestureListener和Detector,它们也可以,但是:

GestureDetector 可以检测到一次投掷,但是在完成其逻辑之后,上下文菜单会打开,这显然不是我想要的。任何想法我做错了什么,或者我可能会做些什么?

4

2 回答 2

8

通常,在检测到onFling后,您需要为视图和所有视图的子视图调用cancelLongPress()方法。但是对于AbsListView这个方法什么都不做 :(

但是我找到了解决方法:在onFling回调中,在处理完所有事情之后,为您的ListView对象执行下一个操作:

Event cancelEvent = Event.obtain(ev2);
cancelEvent.setAction(MotionEvent.ACTION_UP);
listView.onTouchEvent(cancelEvent);
于 2011-02-14T21:37:13.710 回答
6
public class GestureDemoActivity extends ListActivity {

    public void onCreate(Bundle savedInstanceState) {
        // something init code.
        getListView().setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mDetector.onTouchEvent(event)) {
                    MotionEvent cancelEvent = MotionEvent.obtain(event);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                    v.onTouchEvent(cancelEvent);
                    return true;
                }
                return false;
            }
        });
    }
}
于 2012-04-20T10:57:47.443 回答