【问题标题】:Why is my SimpleOnGestureListener not functioning?为什么我的 SimpleOnGestureListener 不起作用?
【发布时间】:2012-06-04 19:16:56
【问题描述】:

我正在使用自己的 GestureDetector 类来检测 left|right 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();
            }
...

【问题讨论】:

  • 您能否验证您的TouchListener 是否正在接收所有要转发的事件?如果您的布局包含也可触摸的子级,则它们可能会从其父级(您的布局)窃取事件。
  • 我认为您可能是正确的。我将把函数放在子布局上看看。

标签: android gesture swipe swipe-gesture onfling


【解决方案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;
            }
        };

【讨论】:

    【解决方案2】:

    我感谢@Devunwired 上面的建议:

    如果布局包含也可触摸的子元素,它们可能是 从其父级(您的布局)窃取事件。

    所以我补充说:

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

    现在效果很好。

    【讨论】:

    • 另一件事你可能会看onInterceptTouchEvent(),这是一种旨在让ViewGroups在他们需要处理他们的孩子以进行滑动等手势时接触他们的方法。
    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 2018-03-05
    • 2013-04-19
    • 2015-10-17
    • 2016-02-20
    • 2023-04-03
    • 2012-01-07
    • 2012-06-11
    相关资源
    最近更新 更多