无法在Android中检测FrameLayout上的手势
我想在android中实现一个全局的NavigationBar Widget,扩展FrameLayout,以便它可以在布局xml中声明,而不是包含在每个活动中。
NavigationBar有三个组件:标志,标题,按钮在NavgationBar(不包括按钮)上触摸或放下时,我会显示一些内容。但是现在我的onFling手势不能被检测到(SwipeGestureListener中的OnFling已经过测试),任何一个可以帮我。
Class NavigationBar:
public class NavigationBarWidget extends FrameLayout implements View.OnClickListener
private SwipeGestureListener mSwipeListener = new SwipeGestureListener();
private GestureDetector mGestureDetector;
NavigationBarWidget():
mGestureDetector = new GestureDetector(mSwipeListener);
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v("Nav", "onTouch");
return mGestureDetector.onTouchEvent(event);
}
});
的OnClick():
@Override
public void onClick(View paramView)
{
if(paramView.getId()== mShareIcon.getId()){
AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}else{
Log.v("Nav", "onClick");
}
}
SwipeGestureListener中的onFling():
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.v("Nav", "onFling");
try {
if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
{
return false;
}
else
{
if ((e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) && (Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY)) {
this.mCallbackListener.onFlingDown();
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Logcat:当触摸或向下滑动navigationBar时,“onTouch”已输出,但未检测到手势onFling。
我自己通过阅读Android开发人员博客上的文章解决了这个问题:让多感触感觉到。
解决方案是让GestureDetector处理手势,然后可以像这样处理触摸事件:
mGestureDetector = new GestureDetector(mSwipeListener);
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v("Nav", "onTouch");
mGestureDetector.onTouchEvent(event);
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:{
Log.v("Nav", "onDown");
break;
}
case MotionEvent.ACTION_UP:{
Log.v("Nav", "onUp");
break;
}
}
return true;
}
});
当我在NavigationBarWidget上向下浏览时,日志将会是:onTouch-> onDown-> onTouch(多少个onTouch直到你保持按下屏幕的时间) - > onFlingDown-> onUp
链接地址: http://www.djcxy.com/p/16211.html上一篇: Can't Detect Gesture on a FrameLayout in Android
下一篇: onFling and onSCroll methods are not detecting on Android Canvas?