本文介绍了手势检测器不​​工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I具有以下GestureListener:

 公共类BookListener扩展SimpleOnGestureListener工具
        OnTouchListener {
    私人LibraryActivity为主;
民营书的书;
私人GestureDetector姿态;

公共BookListener(书书,LibraryActivity主){
    this.main =为主;
    this.book =书;
    手势=新GestureDetector(主,这一点);
}

公共布尔onDoubleTap(MotionEvent E){
    main.showInfo(书);
    返回true;
}

公共布尔onSingleTapConfirmed(MotionEvent E){
    main.openBook(book.getUrl());
    返回true;
}

公共布尔onDown(MotionEvent EVT){
    返回false;
}

@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
    返回gesture.onTouchEvent(事件);
    }
}
 

我把它添加到我的查看方式是这样的:

  view.setOnTouchListener(新BookListener(书,主));
 

但运行时,事件不被触发时,我调试它,我看到onDown叫,但onSingleTapConfirmed或onDoubleTap讷韦尔工作。

怎么了?

我有这个code与工程完美:

 私有类GestureListener扩展SimpleOnGestureListener {
        私人布尔newEvent = TRUE;
        @覆盖
        公共布尔onDoubleTap(MotionEvent E){
            document.rescale();
            refreshImage();
            返回true;
        }

        公共布尔onDown(MotionEvent EVT){
            newEvent = TRUE;
            返回false;
        }

        @覆盖
        公共布尔onScroll(MotionEvent E1,E2 MotionEvent,
                浮distanceX,浮distanceY){
            如果(document.isScaled()及!&安培; newEvent){
                的Vector2Dν=新的Vector2D(E2).minus(新的Vector2D(E1));
                如果(v.getX()→60 || v.getX()&所述-60){
                    如果(v.getX()℃,)
                        下一个();
                    其他
                        previous();
                    newEvent = FALSE;
                }
            } 其他 {
                img.notifyScroll(-distanceX,-distanceY);
                img.invalidate();
            }
            返回true;
        }

        @覆盖
        公共布尔onSingleTapConfirmed(MotionEvent E){
            switchSideBar();
            返回true;
        }
    }
 

解决方案

onDown()必须返回连如果你不希望反应的的事件,否则就会使探测器放弃任何下列事件,因此任何的手势。

I has the following GestureListener:

public class BookListener extends SimpleOnGestureListener implements
        OnTouchListener {
    private LibraryActivity main;
private Book book;
private GestureDetector gesture;

public BookListener(Book book, LibraryActivity main) {
    this.main = main;
    this.book = book;
    gesture = new GestureDetector(main,this);
}

public boolean onDoubleTap(MotionEvent e) {
    main.showInfo(book);
    return true;
}

public boolean onSingleTapConfirmed(MotionEvent e) {
    main.openBook(book.getUrl());
    return true;
}

public boolean onDown(MotionEvent evt){
    return false;
}

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

I add it to my View by this way:

view.setOnTouchListener(new BookListener(book, main));

But when running, the events are not triggered, I debug it, I see the onDown is called, but onSingleTapConfirmed or onDoubleTap nevers works.

What's wrong?

I has this code with works perfectly:

    private class GestureListener extends SimpleOnGestureListener {
        private boolean newEvent = true;
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            document.rescale();
            refreshImage();
            return true;
        }

        public boolean onDown(MotionEvent evt){
            newEvent = true;
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            if (!document.isScaled() && newEvent) {
                Vector2D v = new Vector2D(e2).minus(new Vector2D(e1));
                if (v.getX() > 60 || v.getX() < -60){
                    if (v.getX() < 0)
                        next();
                    else
                        previous();
                    newEvent = false;
                }
            } else {
                img.notifyScroll(-distanceX, -distanceY);
                img.invalidate();
            }
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            switchSideBar();
            return true;
        }
    }
解决方案

onDown() must return true even if you don't want to react to that event, or else it will make the detector discard any following event and hence any gesture.

这篇关于手势检测器不​​工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:21