GestureDetector检测GridView项目上的DoubleClick

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

我想要检测图像GridView中不同项目的双击。 因此,我分配一个单独OnTouchListener到每个商品imageViewgetView()的适配器的方法。 gestureDetector是适配器类的成员变量。

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 {

    @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来解决这两个问题。


 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;
        }
链接地址: http://www.djcxy.com/p/16213.html

上一篇: GestureDetector detect DoubleClick on GridView items

下一篇: Can't Detect Gesture on a FrameLayout in Android