简体   繁体   中英

How to use setOnTouchListener in a fragment?

I have a fragment called ProfileFragment.java which I want to listen for touch gestures by calling a class OnSwipeTouchListener .

Here is a snippet of code from ProfileFragment.java :

rootLayout.setOnTouchListener(new OnSwipeTouchListener(ProfileFragment.this) {

            @Override
            public void onSwipeTop() {
                super.onSwipeTop();
                //folder_gridview.setVisibility(View.INVISIBLE);

                // do something
            }
});

From the above code I get this error:

OnSwipeTouchListener (android.content.Context) in OnSwipeTouchListener cannot be applied to (com.mydomain.sample.ProfileFragment)

Here is the code for OnSwipeTouchListener.java class:

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context ctx) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 1;
        private static final int SWIPE_VELOCITY_THRESHOLD = 1;

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

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                    result = true;
                } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}

I was able to get the above code to work on an activity but cannot get it to work on a fragment. How do I get it to work on a fragment?

You have to pass the context param on your OnSwipeTouchListener , but instead you pass the fragment, ProfileFragment.this which does not inherit from context .

You should pass the getActivity() as param on your OnSwipeTouchListener like this:

rootLayout.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
    /*rest of your code*/ 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM