当用户在屏幕上单击时,我想禁用我显示的按钮。我目前在MainActivity上有一个ViewPager和提到的按钮。
我必须使用哪个监听器来获取单击事件以及我必须在哪里“放”它,所以它不会覆盖我的viewPager的滑动手势? (我试图在我的viewPager上放一个onTouchListener,它确实有效,但我显然无法刷卡了)
我的插图代码(请保持温和,我是Android新手:)):
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener{
(some Code, onCreate etc...)
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG, "Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
triggerButtons();
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return super.onTouchEvent(event);
}
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
triggerButtons();
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
正如您所看到的,当触摸/敲击发生后用户抬起手指时,我希望触发“triggerButtons”。我需要在onCreate中分配什么样的监听器来使其工作?
(如果你也知道如何听取doubleTap,那就太棒了!)
编辑:
我添加了
View.OnTouchListener mOnTouchListener;
GestureDetector mGestureDetector;
我在MainActivity中加入了onCreate:
mOnTouchListener = new View.OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return false;
}
};
和
mViewPager.setOnTouchListener(mOnTouchListener);
解决方案:
on onCreate:
mOnTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
touchEventDecider(event);
return false;
}
};
方法touchEventDecider:
private boolean touchEventDecider(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG, "Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
triggerButtons();
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return super.onTouchEvent(event);
}
}
答案 0 :(得分:2)
将OnTouchListener
添加到ViewPager
。在onTouchEvent
方法的TouchListener
方法中,将每个事件传递给GestureDetector
并返回false
。在GestureDetector
中true
返回onDown
,只留下onSingleTapUp
。
您的问题是,您通过返回onTouchEvent
消费了true
方法中的每一个触摸事件,因此事件不会发送到您的ViewPager
,使其无法滚动。
编辑:如果您想在不使用GestureDetector
的情况下自行识别单击,那么只需将OnTouchListener
附加到您的ViewPager
,但不要忘记在false
方法中返回onTouchEvent
。