Can't Detect Gesture on a FrameLayout in Android
I want to implement a global NavigationBar Widget in android , extends FrameLayout , so that it can just declare in layout xml , not to include in every activity.
The NavigationBar has three components:Logo , Title , Button When touch or fling down on NavgationBar(Not Including Button),I will show something.But now my onFling gesture can`t be detected(OnFling in SwipeGestureListener have been tested),any one can help me.
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");
}
}
onFling() in SwipeGestureListener:
@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:When Touch or Fling Down navigationBar , "onTouch" have been output , but gesture onFling are not detected.
I solved it myself by reading a post on Android Developer Blog: Making Sense of Multitouch.
The Solution is Let GestureDetector handle the gesture, you can handle touch event then like this:
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;
}
});
When I Fling Down over The NavigationBarWidget the log will be: onTouch->onDown->onTouch(how many onTouch up to how long you keep press on the screen)->onFlingDown->onUp
链接地址: http://www.djcxy.com/p/16212.html