无法识别滑动手势

时间:2014-12-05 00:17:57

标签: java android swipe gesture

对于我的Buttons onClickListener,我有以下代码:

on_enter_button.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                enter(words, linearLayout, llp, view);
             }
        });

我只想在任意方向Button屏幕,而不是按下输入Swipe,并运行相同的方法:enter(words, linearLayout, llp, view);

目前我认为我的程序根本无法识别Swipe手势。我认为是因为我错误地放置了enter(words, linearLayout, llp, view);。另外,我将所有Gesture识别码放在onCreate()内。这是编写代码的正确方法吗?

有人可以帮忙吗?

编辑代码:

public class Game extends Activity{

public void enter(String[] w, LinearLayout ll, LinearLayout.LayoutParams params, View v){
... //some code
... //some code
... //some code
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return myGestureDetector.onTouchEvent(event);
    //error myGestureDetector not recognized since it's created in onCreate so it does not exist here
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_activity);

     SimpleOnGestureListener mySimpleGestureListener = new SimpleOnGestureListener()
     {

        @Override
        public boolean onDoubleTap(MotionEvent e) { 
            return super.onDoubleTap(e);
        }

        @Override
        //swipe does not work!
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) 
        {
                enter(words, linearLayout, llp, view);
                return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return super.onSingleTapConfirmed(e);
        }

        private boolean permissibleYVelocity(float velocityY)
        {
            if ((velocityY < -200) || (velocityY > 200))
            {
                return false;
            }
            else
            {
                return true;
            }
        }   
    };

    final GestureDetector myGestureDetector = new  GestureDetector(getApplicationContext(), mySimpleGestureListener);

    //button listener works
    on_enter_button.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    enter(words, linearLayout, llp, view);
                 }
            });
    }

}

1 个答案:

答案 0 :(得分:2)

您需要覆盖View.onTouchEvent()

@Override
public boolean onTouchEvent(MotionEvent event) {
    return mGestureDetector.onTouchEvent(event);
}

在SimpleOnGestureListener.onFling()中调用您的方法:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) 
{
    enter(words, linearLayout, llp, view);
    return true;
}

编辑: 在onCreate()中设置你的GestureDectector:

mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener()
{
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return super.onDoubleTap(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
    {
        enter(words, linearLayout, llp, view);
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        super.onLongPress(e);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return super.onSingleTapConfirmed(e);
    }

    private boolean permissibleYVelocity(float velocityY)
    {
        if ((velocityY < -200) || (velocityY > 200))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
});