为什么我的SimpleOnGestureListener不能正常工作? [英] Why is my SimpleOnGestureListener not functioning?

查看:171
本文介绍了为什么我的SimpleOnGestureListener不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我自己的GestureDetector类检测左|右onFling事件。我看到的一切看起来在我的code不错,但没有任何反应......?

我需要超出了我的切换按钮附加功能打开|关闭我的片段导航视图。在我AnimationLayout类中的方法的切换调用如下:

 公共无效toggleSidebar()
{
如果(mContent.getAnimation()!= NULL)
{
返回;
}

如果(mOpened)
{
/ *打开,使关闭动画* /
mAnimation =新TranslateAnimation(0,-mSidebarWidth,0,0);
mAnimation.setAnimationListener(mCloseListener);
} 其他
{
/ *不开,让开动画* /
mAnimation =新TranslateAnimation(0,mSidebarWidth,0,0);
mAnimation.setAnimationListener(mOpenListener);
}
mAnimation.setDuration(持续时间);
mAnimation.setFillAfter(真正的);
mAnimation.setFillEnabled(真正的);
mContent.startAnimation(mAnimation);
}
 

 公共无效openSidebar()
    {
    如果(!mOpened)
    {
    toggleSidebar();
    }
    }

    公共无效closeSidebar()
    {
    如果(mOpened)
    {
    toggleSidebar();
    }
    }
 

在我的主要活动的onFling()上的切换子称:

 公共静态AnimationLayout mLayout;

    //这些常量用于onFling
    私有静态最终诠释SWIPE_MIN_DISTANCE = 120;
    私有静态最终诠释SWIPE_MAX_OFF_PATH = 250;
    私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
    私人GestureDetector gestureDetector;

@覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        mLayout =(AnimationLayout)findViewById(R.id.animation_layout);
        gestureDetector =新GestureDetector(this.getApplicationContext(),新MyGestureDetector());
        //设置触摸侦听器的主要观点是我们的自定义手势
        //监听器
        mLayout.setOnTouchListener(新View.OnTouchListener(){
            公共布尔onTouch(视图V,MotionEvent事件){
                如果(gestureDetector.onTouchEvent(事件)){
                    返回true;
                }
                返回false;
            }
        });
    }
      类MyGestureDetector扩展SimpleOnGestureListener {
        @覆盖
        公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){

            如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH){
                返回false;
            }
            //从右向左轻扫
            如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE&安培;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                Toast.makeText(getApplicationContext(),RIGHT_LEFT,Toast.LENGTH_LONG).show();
                Log.i(MyGestureDetector,R2L!);
                mLayout.closeSidebar();
                //从左向右轻扫
            }否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE和放大器;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                Toast.makeText(getApplicationContext(),LEFT_RIGHT,Toast.LENGTH_LONG).show();
                Log.i(MyGestureDetector,L2R!);
                mLayout.openSidebar();
            }

            返回false;
        }

        //返回true从onDown的onFling事件登记
        @覆盖
        公共布尔onDown(MotionEvent E){
            返回true;
        }
    }
 

此按钮正常工作:

  ...
案例R.id.navToggleBtn:{
                mLayout.toggleSidebar();
            }
...
 

解决方案

我给信贷@Devunwired,因为他上面的建议:

  

如果布局包含的孩子,也可触时,它们可以   窃取他们的父母(布局)的事件。

于是我说:

 查看swipeView =(查看)findViewById(R.id.animation_layout_content);
swipeView .setOnTouchListener(新View.OnTouchListener(){...});
 

现在奇妙的作品。

I'm using my own GestureDetector class to detect a left|right onFling event. Everything I see looks good in my code but nothing happens...?

I need the added functionality beyond my toggle button that opens|closes a navigation view in my fragments. The toggle calls on a method in my AnimationLayout class as follows:

public void toggleSidebar()
{
if (mContent.getAnimation() != null)
{
return;
}

if (mOpened)
{
/* opened, make close animation */
mAnimation = new TranslateAnimation(0, -mSidebarWidth, 0, 0);
mAnimation.setAnimationListener(mCloseListener);
} else
{
/* not opened, make open animation */
mAnimation = new TranslateAnimation(0, mSidebarWidth, 0, 0);
mAnimation.setAnimationListener(mOpenListener);
}
mAnimation.setDuration(DURATION);
mAnimation.setFillAfter(true);
mAnimation.setFillEnabled(true);
mContent.startAnimation(mAnimation);
}

and...

public void openSidebar()
    {
    if (!mOpened)
    {
    toggleSidebar();
    }
    }

    public void closeSidebar()
    {
    if (mOpened)
    {
    toggleSidebar();
    }
    }

Within my Main Activity the onFling() calls on the toggle sub:

public static AnimationLayout mLayout;

    // these constants are used for onFling
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLayout = (AnimationLayout) findViewById(R.id.animation_layout);
        gestureDetector = new GestureDetector(this.getApplicationContext(), new MyGestureDetector());
        // Set the touch listener for the main view to be our custom gesture
        // listener
        mLayout.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });
    }
      class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                return false;
            }
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getApplicationContext(), "right_left", Toast.LENGTH_LONG).show();
                Log.i("MyGestureDetector", "R2L!");
                mLayout.closeSidebar();
                // left to right swipe
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getApplicationContext(), "left_right", Toast.LENGTH_LONG).show();
                Log.i("MyGestureDetector", "L2R!");
                mLayout.openSidebar();
            }

            return false;
        }

        // Return true from onDown for the onFling event to register
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }

This Button works fine:

...
case R.id.navToggleBtn : {
                mLayout.toggleSidebar();
            }
...

解决方案

I give credit to @Devunwired as he suggested above:

If layout contains children that are also touchable, they may be stealing the events from their parent (your layout).

So I added:

View swipeView = (View) findViewById(R.id.animation_layout_content);
swipeView .setOnTouchListener(new View.OnTouchListener() {...});

Which works wonderfully now.

这篇关于为什么我的SimpleOnGestureListener不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆