onSingleTapConfirmed从未触发

时间:2019-02-23 16:08:20

标签: java android ontouchlistener ontouchevent gesturedetector

我正在尝试实现点击监听功能,但是由于某种原因,onSingleTapConfirmed方法从未触发,我也不知道为什么。

我在另一个项目中仅用两个按钮尝试过此操作,但在那里实现它没有问题,但是由于某些原因,即使代码相同,该应用程序也无法正常工作。

MyGestureDetector.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 /手势:   OnDOwn检测到2019-02-23 17:51:25.612 7177-7177 / package   D /手势:OnDown检测到2019-02-23 17:51:26.190   7177-7177 /程序包D /手势:检测到OnDown 2019-02-23   17:51:26.718 7177-7177 /程序包D /手势:检测到OnDown   2019-02-23 17:51:26.869 7177-7177 / package 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 / Gesture包:最多   滑动: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 /手势:检测到OnDown 2019-02-23   17:51:29.414 7177-7177 / D / Gesture包:最多   刷卡:8.0-272.0 2019-02-23 17:51:30.584   7177-7177 /程序包D /手势:检测到OnDown 2019-02-23   17:51:35.959 7177-7177 /软件包D /手势:检测到OnDown   2019-02-23 17:51:36.356 7177-7177 / package D /手势:   OnDOwn检测到2019-02-23 17:51:36.737 7177-7177 / package   D /手势:检测到OnDown

正如您所看到的,即使在onSingleTapConfirmed之前没有轻扫的情况下,即使我要做的只是轻按,也不会触发OnDown

编辑 是否做了一些测试记录了动作,问题是手势检测器出于某种原因看不到我的任何视图,除了layout,因为onSingleTapConfirmed在任何文本视图之外都被触发,并且如果我禁用了他们的听众。 伙计们,我真的需要帮助!

2 个答案:

答案 0 :(得分:0)

似乎其他视图由于某种原因无法点击。

在添加以下内容到添加侦听器之后,代码现在可以按预期工作:

<div class="questionholder" id="question5">
  <div>
    <h5>Select all elements</h5>
    <input class="input5" type="checkbox" id="elementName" name="element" value="name"><label for="elementName"><p class="radioChoice">Name</p></label>
    <input class="input5" type="checkbox" id="elementPhoto" name="element" value="photo"><label for="elementPhoto"><p class="radioChoice">Photo</p></label>
    <input class="input5" type="checkbox" id="elementSignature" name="element" value="signature"><label for="elementSignature"><p class="radioChoice">Signature</p></label>
  </div>
  <div class="holdButtons">
    <a class="text2button" onclick="admissible()">Next</a>
  </div>
</div>

答案 1 :(得分:0)

返回GestureDetector的{​​{1}}提供的值:

onTouchEvent