Android ListView 与 OnItemClickListener 和 GestureDetector

发布于 2024-10-15 00:15:54 字数 2509 浏览 6 评论 0原文

我有以下 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可以正常检测到一次猛击,但是在完成其逻辑后,上下文菜单会打开,这显然不是我想要的。有什么想法我做错了吗,或者我可以采取什么措施?

I have a the following 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;
        }
    }   
} 

The contextListener (long-click) on the listItems works perfectly. Today i added the gestureListener and Detector, which works too, BUT:

The GestureDetector detects a fling all right, but after it's done with its logic, the context menu opens, which is obviously not what i want. Any ideas what i'm doing wrong, or what i might do about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-10-22 00:15:54

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

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

Event cancelEvent = Event.obtain(ev2);
cancelEvent.setAction(MotionEvent.ACTION_UP);
listView.onTouchEvent(cancelEvent);

Normally, you need to call cancelLongPress() method for view and all view's children after onFling has been detected. But for AbsListView this method do nothing :(

But I've found workaround for it: in the onFling callback, after all things were processed, do the next for your ListView object:

Event cancelEvent = Event.obtain(ev2);
cancelEvent.setAction(MotionEvent.ACTION_UP);
listView.onTouchEvent(cancelEvent);
尐偏执 2024-10-22 00:15:54
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;
            }
        });
    }
}
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;
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文