关于java:GestureDetector检测GridView项目上的DoubleClick

GestureDetector detect DoubleClick on GridView items

注意:此特定问题已解决,但存在严重的后续问题。看一下GestureDetector - 检测双击GridView项目虽然在onTouchEvent()中返回false

我想检测GridView图像中不同项目的双击。
因此,我为适配器的getView()方法中的每个项目imageView分配了一个单独的OnTouchListenergestureDetector是适配器类的成员变量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private GestureDetectorCompat gestureDetector;

public ImageGridViewAdapter(Context c, ArrayList<UriWrapper> startUpImages)     {
    mContext = c;
    uriManager  = new UriManager(startUpImages);
    gestureDetector = new GestureDetectorCompat(mContext, new SingleTapConfirm());
}

public View getView(final int position, View recycled, ViewGroup parent) {

    ViewHolder holder;
    if (recycled == null) {
       ..... find items by id
    } else{
        holder = (ViewHolder) recycled.getTag();
    }

    // Set listener to item image
    holder.image.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Always returns false, the gestureDetector does not detect anything
            boolean ret = gestureDetector.onTouchEvent(event);
            // At least the onTouch-callback gets called with the correct position
            Log.e(TAG,"onTouch returned" + ret +" at position" + position);
            return true;
        }
    });

    // Use glide library to load images into the image views
    Glide.with(mContext)....into(holder.image);
    return recycled;
}

私有类SingleTapConfirm扩展了GestureDetector.SimpleOnGestureListener {

1
2
3
4
5
6
7
8
9
10
11
12
    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
        Log.e(TAG,"onSingleTapConfirmed"); // never called..
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.e(TAG,"onDoubleTap"); // never called..
        return super.onDoubleTap(e);
    }
}

OnTouchListener的工作并以正确的位置调用。
但是,无论我在做什么,都不会调用gestureDetector的方法。这段代码似乎有什么问题?

更新:onTouch回调需要返回true,现在至少GestureDetector工作。但是,返回true会破坏其余的功能,因为我有一个长按选择模式和一个用于我的GridView的全局OnTouchListener。

第二次更新:
合并特定于项目的OnTouchListener和全局OnTouchListener无法正常工作。 (仅在某些项目上识别滑动手势)
我希望通过创建自定义View扩展ImageView并在那里分配特定于项目的OnTouchListener,我可以解决这两个问题。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 private GestureDetectorCompat gestureDetector;

    // in your adapter constructor
    gestureDetector = new GestureDetector(context, new SingleTapConfirm());

    public View getView(final int position, View recycled, ViewGroup parent) {

        ViewHolder holder;
        if (recycled == null) {
            .....find items by id
        } else {
            holder = (ViewHolder) recycled.getTag();
        }

        // Set listener to item image
        holder.image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Use lazy initialization for the gestureDetector
                gestureDetector.onTouchEvent(event);
                // At least the onTouch-callback gets called with the correct position
                return true;
            }
        });

        // Use glide library to load images into the image views
        Glide.with(mContext)....into(holder.image);
        return recycled;
    }

    private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Log.e(TAG,"onSingleTapConfirmed"); // never called..
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.e(TAG,"onDoubleTap"); // never called..
            return super.onDoubleTap(e);
        }
    }

<但是>
更新:
<铅>

1
2
3
4
5
        @Override
        public boolean onTouch(View v, MotionEvent event) {anything
            gestureDetector.onTouchEvent(event);
            return true;
        }