[英]Swipe gesture not recognized
For my Buttons
onClickListener
I have the following code: 对于我的Buttons
onClickListener
我具有以下代码:
on_enter_button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
enter(words, linearLayout, llp, view);
}
});
Instead of pressing enter Button
, I simply want to Swipe
the screen in any direction and run the same method: enter(words, linearLayout, llp, view);
而不是按Enter Button
,我只想向任何方向Swipe
屏幕并运行相同的方法: enter(words, linearLayout, llp, view);
Currently I think my program does not recognize Swipe
gestures at all. 目前,我认为我的程序根本无法识别Swipe
手势。 I assume it's because I placed enter(words, linearLayout, llp, view);
我认为这是因为我放置了enter(words, linearLayout, llp, view);
incorrectly. 错误地。 Also, I put all the Gesture
recognition code inside the onCreate()
. 另外,我将所有Gesture
识别代码放在onCreate()
。 Is that the correct way to program the code? 那是编写代码的正确方法吗?
Can someone please help? 有人可以帮忙吗?
EDITED CODE: 编辑代码:
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);
}
});
}
} }
You need to override View.onTouchEvent()
. 您需要重写View.onTouchEvent()
。
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
Call your method in SimpleOnGestureListener.onFling(): 在SimpleOnGestureListener.onFling()中调用您的方法:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
enter(words, linearLayout, llp, view);
return true;
}
EDIT: Setup your GestureDectector in onCreate() as well: 编辑:在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.