为什么我的SimpleOnGestureListener没有发挥作用?(Why is my Simp

2019-07-29 17:34发布

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

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

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

和...

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

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

在我的主要活动的onFling()调用切换子:

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

该按钮正常工作:

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

Answer 1:

我认为其中一个原因是对手势的工作onFling这一条件不满足于我们的代码我已经实现了一个OntouchListener试试这个

yourView.setOnTouchListner(onThumbTouch );




    OnTouchListener onThumbTouch = new OnTouchListener()
        {
            float previouspoint = 0 ;
            float startPoint=0;
            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {   
                switch(v.getId())
                {
                case R.id.tvDetailsalaujairiyat: // Give your R.id.sample ...
                {
                    switch(event.getAction())
                    {
                    case MotionEvent.ACTION_DOWN:
                    {          
                        startPoint=event.getX();
                        System.out.println("Action down,..."+event.getX());
                    }
                    break;
                    case MotionEvent.ACTION_MOVE:
                    {       

                    }break;
                    case MotionEvent.ACTION_CANCEL:
                    {                           

                        previouspoint=event.getX();
                        if(previouspoint > startPoint){
                            //Right side swape

                        }else{
                        // Left side swape
                        }

                    }break;

                    }
                    break;
                }
                }
                return true;
            }
        };


Answer 2:

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

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

于是我说:

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

这奇妙的现在工作。



文章来源: Why is my SimpleOnGestureListener not functioning?