簡體   English   中英

滑動手勢無法識別

[英]Swipe gesture not recognized

對於我的Buttons onClickListener我具有以下代碼:

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

而不是按Enter 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);
                 }
            });
    }

}

您需要重寫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;
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM