简体   繁体   English

具有onItemLongclick的GestureDetector

[英]GestureDetector with onItemLongclick

How can i integrate GestureDetector with onItemLongClick? 如何将GestureDetector与onItemLongClick集成?

I have a GridView containing three images. 我有一个包含三个图像的GridView。 When I touch on the first image, I want to display a Toast message. 当我触摸第一张图像时,我想显示一条Toast消息。 When I lift up my finger from the screen, I want to display a second Toast message. 当我从屏幕上抬起手指时,我想显示第二条Toast消息。

I know that GestureDetector uses MotionEvent, but onItemLongClick does not. 我知道GestureDetector使用MotionEvent,但onItemLongClick却没有。 But in this case, I would need to keep track of the image's position ID in the grid, thus it is not possible to implement inside onTouch()? 但是在这种情况下,我需要跟踪图像在网格中的位置ID,因此无法在onTouch()内部实现?

You can track the motion of the cursor, whether its a onscreen touch or a TrackBall move, using this class and if it crosses over into the next picture you can handle that event then. 您可以使用此类跟踪光标的运动,无论是屏幕触摸还是TrackBall移动,如果光标越过下一幅图片,则可以处理该事件。 Here is an example taken from the sdk examples: 这是取自sdk示例的示例:

    @Override public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();
        if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL) {
            int N = event.getHistorySize();
            int P = event.getPointerCount();
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < P; j++) {
                    mCurX = event.getHistoricalX(j, i);
                    mCurY = event.getHistoricalY(j, i);
                    drawPoint(mCurX, mCurY,
                            event.getHistoricalPressure(j, i),
                            event.getHistoricalTouchMajor(j, i));
                }
            }
            for (int j = 0; j < P; j++) {
                mCurX = event.getX(j);
                mCurY = event.getY(j);
                drawPoint(mCurX, mCurY, event.getPressure(j), event.getTouchMajor(j));
            }
        }
        return true;
    }

You can read more and see the file in your SDK at C:\\YourInstallDir\\android-sdk\\samples\\android-10\\ApiDemos\\src\\com\\example\\android\\apis\\graphics\\TouchPaint.java or just search the whole examples files for MotionEvent there's a few more uses in them. 您可以在C:\\ YourInstallDir \\ android-sdk \\ samples \\ android-10 \\ ApiDemos \\ src \\ com \\ example \\ android \\ apis \\ graphics \\ TouchPaint.java中阅读更多内容并查看SDK中的文件,或者仅搜索整个示例MotionEvent的文件还有更多用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM