我正在尝试实现点击监听器以进行点击和投掷,但由于某种原因,onSingleTapConfirmed
方法永远不会被触发,我真的不知道为什么。
我在一个不同的项目中尝试了这个只有两个按钮,我在那里实现它没有任何问题,但由于某些原因在这个应用程序中,即使代码是相同的,它也不起作用。
GestureDetector.class:
公共类MyGestureDetector扩展GestureDetector.SimpleOnGestureListener {
private MainGame mainGame;
MyGestureDetector(MainGame mainGame) {
this.mainGame = mainGame;
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("GESTURE", "OnDOwn detected");
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("GESTURE: ", "Single Tap detected");
mainGame.clickedView();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (e1.getY() < e2.getY()) {
Log.d("Gesture", "Up to Down swipe: " + e1.getY() + " - " + e2.getY());
Log.d("Speed", String.valueOf(velocityY) + "pixels/second");
Objects.requireNonNull(mainGame.getSupportActionBar()).show();
}
if (e1.getY() > e2.getY()) {
Log.d("Gesture", "Down to Up swipe: " + e1.getY() + " - " + e2.getY());
Log.d("Speed", String.valueOf(velocityY) + "pixels/second");
Objects.requireNonNull(mainGame.getSupportActionBar()).hide();
}
} catch (NullPointerException e) {
Log.d("ActionBar: ", "Nullpoint caught");
}
return true;
}
}
MainGame.class(片段):
public class MainGame extends AppCompatActivity implements View.OnTouchListener {
ConstraintLayout layout;
TextView fullNameP1;
TextView fullNameP2;
TextView gameScoreP1;
TextView gameScoreP2;
View mCurrenView;
private MyGestureDetector = myGestureDetector;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
Objects.requireNonNull(getSupportActionBar()).hide();
setContentView(R.layout.activity_main_game);
initializeLayoutViews();
initializeLayoutValues();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE;
layout.setSystemUiVisibility(uiOptions);
layout.setOnTouchListener(this);
gameScoreP1.setOnTouchListener(this);
gameScoreP2.setOnTouchListener(this);
fullNameP1.setOnTouchListener(this);
fullNameP2.setOnTouchListener(this);
}
private void initializeLayoutViews() {
layout = findViewById(R.id.gameLayout);
mainLayout.clone(layout);
fullNameP1 = findViewById(R.id.fullNameP1);
fullNameP2 = findViewById(R.id.fullNameP2);
gameScoreP1 = findViewById(R.id.gameScoreP1);
gameScoreP2 = findViewById(R.id.gameScoreP2);
myGestureDetector = new MyGestureDetector(this);
mGestureDetector = new GestureDetector(this, myGestureDetector);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
mCurrenView = v;
return this.mGestureDetector.onTouchEvent(event);
}
void clickedView() {
switch(mCurrenView.getId()) {
//perform actions
}
}
}
这是我在屏幕上任何地方的日志:
2019-02-23 17:51:24.801 7177-7177 /包D / GESTURE:OnDOwn检测到2019-02-23 17:51:25.612 7177-7177 /包D / GESTURE:OnDOwn检测到2019-02-23 17:51 :26.190 7177-7177 /包装D / GESTURE:OnDOwn检测到2019-02-23 17:51:26.718 7177-7177 /包装D / GESTURE:OnDOwn检测到2019-02-23 17:51:26.869 7177-7177 /包D /手势:向下向上滑动:275.33337 - -5.333313 2019-02-23 17:51:27.364 7177-7177 /包D /手势:OnDOwn检测到2019-02-23 17:51:27.575 7177-7177 /包D /手势:向上向下滑动:466.6667 - 772.3962 2019-02-23 17:51:28.438 7177-7177 /包D /手势:OnDOwn检测到2019-02-23 17:51:28.643 7177-7177 /包D /手势:向下滑动:215.33337 - -52.07727 2019-02-23 17:51:29.224 7177-7177 /包装D / GESTURE:OnDOwn检测到2019-02-23 17:51:29.414 7177-7177 /包装D /手势:向上向下滑动:8.0 - 272.0 2019-02-23 17:51:30.584 7177-7177 /包装D / GESTURE:OnDOwn检测到2019-02-23 17:51:35.959 7177-7177 /包装D / GESTURE:OnDOwn检测到2019 -02-23 17:51:36 .356 7177-7177 / package D / GESTURE:OnDOwn检测到2019-02-23 17:51:36.737 7177-7177 / package D / GESTURE:OnDOwn检测到
正如你所看到的那样,onSingleTapConfirmed
永远不会被触发,即使我所做的只是一个简单的点击,当OnDown
没有滑动之前。
编辑做了一些测试记录动作,问题是手势检测器除了layout
之外没有看到我的任何视图,因为onSingleTapConfirmed
在任何文本视图之外被触发,如果我禁用它们的监听器。伙计们,我真的需要帮助!
似乎其他视图由于某种原因无法点击。
在添加以下内容之后,代码按照预期工作:添加侦听器之前:
gameScoreP1.setClickable(true);
gameScoreP2.setClickable(true);
fullNameP1.setClickable(true);
fullNameP2.setClickable(true);
返回GestureDetector
的onTouchEvent
提供的值:
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.mGestureDetector.onTouchEvent(event);
}