实现对view的单击双击监听

来源:互联网 发布:java与node.js交互 编辑:程序博客网 时间:2024/05/26 15:59

这里只介绍使用方法:

1.实现一个GestureDetector监听器

2.设置setOnDoubleTapListener监听

3.实现onDoubleTap(执行双击操作)

4.实现onSingleTapConfirmed(执行单击操作)


// 手势监听器

GestureDetector  mGestureDetector = new GestureDetector(imageView.getContext(),
                    new GestureDetector.SimpleOnGestureListener() {


                        // forward long click listener
                        @Override
                        public void onLongPress(MotionEvent e) {
                            // 可执行长按操作
                        }
                    });


// 实现双击监听(其中包含了单击的监听)
            mGestureDetector.setOnDoubleTapListener(this);


public final boolean onDoubleTap(MotionEvent ev) {

// 执行双击事件

}


// 执行单击事件

public final boolean onSingleTapConfirmed(MotionEvent e) {


// 以下方法判断是单击在指定view上或是view外部

//用imageview为例

        ImageView imageView = getImageView();

// 单击imageview

        if (null != imageView) {

     if (null != mPhotoTapListener) { // 单击imageview监听器

                final RectF displayRect = getDisplayRect();

                if (null != displayRect) {
                    final float x = e.getX(), y = e.getY();


                    // Check to see if the user tapped on the photo
                    if (displayRect.contains(x, y)) {


                        float xResult = (x - displayRect.left)
                                / displayRect.width();
                        float yResult = (y - displayRect.top)
                                / displayRect.height();

// 单击imageview 回调方法
                        mPhotoTapListener.onPhotoTap(imageView, xResult,
                                yResult);
                        return true;
                    }
                }

            }

// 单击imageview外部监听

            if (null != mViewTapListener) {
               //执行单击imageview外部事件
            }
        }
        return false;
    }
1 0
原创粉丝点击