GestureDetector 检测 GridView 项目上的双击

GestureDetector detect DoubleClick on GridView items

注:这个具体问题解决了,但是后续问题严重。看看 GestureDetector - Detect double click in GridView item's although returning false in onTouchEvent()

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

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

私有 class SingleTapConfirm 扩展 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);
    }
}

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

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

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

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

更新:

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