不能在Android上的FrameLayout检测手势 [英] Can't Detect Gesture on a FrameLayout in Android

查看:586
本文介绍了不能在Android上的FrameLayout检测手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个全球性的导航栏小工具在Android中,延伸的FrameLayout,以便它可以只是声明布局的XML,而不是在每一个活动包括

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.

在导航栏有三个组成部分:标志,标题,按钮
当触摸或一扔下来NavgationBar(不包括按钮),我将展示something.But现在我onFling姿态不能老是被检测(OnFling在SwipeGestureListener已测试),任何一个可以帮助我。

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.

类导航栏:

public class NavigationBarWidget extends FrameLayout implements View.OnClickListener

private SwipeGestureListener mSwipeListener = new SwipeGestureListener();
private GestureDetector mGestureDetector;

NavigationBarWidget():

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():

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()在SwipeGestureListener:

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的:当触摸或弗林向下导航栏,onTouch已输出,但没有检测到手势onFling

Logcat:When Touch or Fling Down navigationBar , "onTouch" have been output , but gesture onFling are not detected.

推荐答案

我通过阅读在Android开发者博客中解决了它自己:
制作多点触控感。

I solved it myself by reading a post on Android Developer Blog: Making Sense of Multitouch.

解决方案就是让GestureDetector处理的手势,你可以处理触摸事件则是这样的:

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;
        }
    });

当我下来一扔在NavigationBarWidget日志将是:
onTouch-> onDown-> onTouch(多少onTouch给你多长时间保持屏幕上preSS) - > onFlingDown-> onUp

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

这篇关于不能在Android上的FrameLayout检测手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆