检测的片段轻扫手势手势、片段

2023-09-06 03:44:01 作者:床上情人

您好我实现了一个片段,我想检测触摸事件对我的片段。

不过,我无法检测到该事件,因为事实上,没有任何事件beign在所有检测到的。它的工作原理以及在活动,但它不工作的片段。

以下是我的code:

 公共类Swipe_Fragment扩展片段工具
    GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener {

私人SimpleGestureFilter检测器;
私人的LinearLayout swipLinear;
私有静态整数默认= 50;
私人诠释亮度;
私人GestureDetectorCompat mDetector;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.swipe_screen,集装箱,
            假);
    swipLinear =(的LinearLayout)rootView.findViewById(R.id.swipLinear);

    亮度=默认值;
    mDetector =新GestureDetectorCompat(getActivity(),这一点);
    //设置手势检测为双击
    //监听器。
    mDetector.setOnDoubleTapListener(本);
    //检测触摸区域
    //探测器=新SimpleGestureFilter(getActivity(),这一点);
    返回rootView;
}


@覆盖
公共布尔onDoubleTap(MotionEvent为arg0){
    Toast.makeText(getActivity(),onDoubleTap,Toast.LENGTH_LONG).show();
    返回false;
}

@覆盖
公共布尔onDoubleTapEvent(MotionEvent E){
    Toast.makeText(getActivity(),onDoubleTapEvent,Toast.LENGTH_LONG).show();
    返回false;
}

@覆盖
公共布尔onSingleTapConfirmed(MotionEvent E){
    Toast.makeText(getActivity(),onSingleTapConfirmed,Toast.LENGTH_LONG).show();
    返回false;
}

@覆盖
公共布尔onDown(MotionEvent E){
    Toast.makeText(getActivity(),onDown,Toast.LENGTH_LONG).show();
    返回false;
}

@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
        浮动velocityY){
    Toast.makeText(getActivity(),onFling,Toast.LENGTH_LONG).show();
    返回false;
}

@覆盖
公共无效onLong preSS(MotionEvent E){
    Toast.makeText(getActivity(),onLong preSS的,Toast.LENGTH_LONG).show();

}

@覆盖
公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,
        浮动distanceY){
    Toast.makeText(getActivity(),onScroll,Toast.LENGTH_LONG).show();
    返回false;
}

@覆盖
公共无效OnShow中preSS(MotionEvent E){
    Toast.makeText(getActivity(),OnShow中preSS的,Toast.LENGTH_LONG).show();

}

@覆盖
公共布尔onSingleTapUp(MotionEvent E){
    Toast.makeText(getActivity(),onSingleTapUp,Toast.LENGTH_LONG).show();
    返回false;
}

}
 

解决方案

最后,我从code以下,这是从Android片段onCreateView用手势。

 最后GestureDetector姿态=新GestureDetector(getActivity()
    新GestureDetector.SimpleOnGestureListener(){

        @覆盖
        公共布尔onDown(MotionEvent E){
            返回true;
        }

        @覆盖
        公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
            浮动velocityY){
            Log.i(Constants.APP_TAG,onFling被称为!);
            最终诠释SWIPE_MIN_DISTANCE = 120;
            最终诠释SWIPE_MAX_OFF_PATH = 250;
            最终诠释SWIPE_THRESHOLD_VELOCITY = 200;
            尝试 {
                如果(Math.abs(e1.getY() -  e2.getY())> SWIPE_MAX_OFF_PATH)
                    返回false;
                如果(e1.getX() -  e2.getX()> SWIPE_MIN_DISTANCE
                    &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                    Log.i(Constants.APP_TAG,从右至左);
                }否则,如果(e2.getX() -  e1.getX()> SWIPE_MIN_DISTANCE
                    &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                    Log.i(Constants.APP_TAG,左到右);
                }
            }赶上(例外五){
                //什么
            }
            返回super.onFling(E1,E2,velocityX,velocityY);
        }
    });

v.setOnTouchListener(新View.OnTouchListener(){
    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        返回gesture.onTouchEvent(事件);
    }
});
 

这正好在片段的 onCreateView()方法。

苹果 Apple Watch 将很快支持手势操控

Hi I have implemented a Fragment, and I want to detect touch event on my fragment.

But I am unable to detect that event, as a matter of fact, no event is beign detected at all. It works well in Activity, but it's not working on fragments.

Following is my code:

  public class Swipe_Fragment extends Fragment implements
    GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

private SimpleGestureFilter detector;
private LinearLayout swipLinear;
private static int DEFAULT = 50;
private int brightness;
private GestureDetectorCompat mDetector;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.swipe_screen, container,
            false);
    swipLinear = (LinearLayout) rootView.findViewById(R.id.swipLinear);

    brightness = DEFAULT;
    mDetector = new GestureDetectorCompat(getActivity(), this);
    // Set the gesture detector as the double tap
    // listener.
    mDetector.setOnDoubleTapListener(this);
    // Detect touched area
    // detector = new SimpleGestureFilter(getActivity(), this);
    return rootView;
}


@Override
public boolean onDoubleTap(MotionEvent arg0) {
    Toast.makeText(getActivity(), "onDoubleTap", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    Toast.makeText(getActivity(), "onDoubleTapEvent", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    Toast.makeText(getActivity(), "onSingleTapConfirmed", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onDown(MotionEvent e) {
    Toast.makeText(getActivity(), "onDown", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    Toast.makeText(getActivity(), "onFling", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
    Toast.makeText(getActivity(), "onLongPress", Toast.LENGTH_LONG).show();

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Toast.makeText(getActivity(), "onScroll", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public void onShowPress(MotionEvent e) {
    Toast.makeText(getActivity(), "onShowPress", Toast.LENGTH_LONG).show();

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    Toast.makeText(getActivity(), "onSingleTapUp", Toast.LENGTH_LONG).show();
    return false;
}

}

解决方案

Finally I got a solution from the code below, which is from Android Fragment onCreateView with Gestures.

  final GestureDetector gesture = new GestureDetector(getActivity(),
    new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
            Log.i(Constants.APP_TAG, "onFling has been called!");
            final int SWIPE_MIN_DISTANCE = 120;
            final int SWIPE_MAX_OFF_PATH = 250;
            final int SWIPE_THRESHOLD_VELOCITY = 200;
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.i(Constants.APP_TAG, "Right to Left");
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.i(Constants.APP_TAG, "Left to Right");
                }
            } catch (Exception e) {
                // nothing
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    });

v.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gesture.onTouchEvent(event);
    }
});

This goes in the onCreateView() method of the fragment.