Android swipe left and right in RelativeLayout
Asked Answered
B

2

5

I am trying to implement swipe left or right in my RelativeLayout. I wrote some code but could not get swipe left or right working. I am using GestureDetector this is a my source

private GestureDetector gesturedetector = null;
private RelativeLayout swipelayout;

@SuppressLint("UseValueOf")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.strada_menu_result_loadmore,
            container, false);


    swipelayout = (RelativeLayout) rootView.findViewById(R.id.swipelayout);

    gesturedetector = new GestureDetector(new MyGestureListener());
    swipelayout.setOnTouchListener(new OnTouchListener() {

        @Override

        public boolean onTouch(View v, MotionEvent event) {

        gesturedetector.onTouchEvent(event);

        return true;

        }

        });




    return rootView;

}


public boolean dispatchTouchEvent(MotionEvent ev){



    return gesturedetector.onTouchEvent(ev);
}



class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_MIN_DISTANCE = 150;

    private static final int SWIPE_MAX_OFF_PATH = 100;

    private static final int SWIPE_THRESHOLD_VELOCITY = 100;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        float dX = e2.getX() - e1.getX();

        float dY = e1.getY() - e2.getY();

        if (Math.abs(dY) >= SWIPE_THRESHOLD_VELOCITY
                && Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY &&

                Math.abs(dX) >= SWIPE_MIN_DISTANCE) {

            if (dX > 0) {

                Toast.makeText(getActivity(), "Right Swipe",
                        Toast.LENGTH_SHORT).show();

            } else {

                Toast.makeText(getActivity(), "Left Swipe",
                        Toast.LENGTH_SHORT).show();

            }

            return true;

        }
        return false;

    }
}

what am i doing wrong? if anyone knows solution please help me thanks

Bracey answered 18/7, 2014 at 14:40 Comment(4)
I suggest you use ViewPager for swiping.Kwangtung
@Kwangtung I assume he just wants to detect swipes and not page through Fragments. Why else would he use a GestureDetector?Alimony
yes i simple want to detect swipe only fragmentBracey
Help us out a little here then. What are you trying to do? What exactly does your code do that you don't want?Kwangtung
P
8

You just need an OnTouchListener, that should be all for simple answer. Take a look. I used this code in some working project, so everything should work fine. But let me know if there is something missing and doesn't work.

yourRelativeLayout.setOnTouchListener(new OnTouchListener() {

    int downX, upX;

     @Override
     public boolean onTouch(View v, MotionEvent event) {

         if (event.getAction() == MotionEvent.ACTION_DOWN) {
             downX = (int) event.getX(); 
             Log.i("event.getX()", " downX " + downX);
             return true;
         } 

         else if (event.getAction() == MotionEvent.ACTION_UP) {
             upX = (int) event.getX(); 
             Log.i("event.getX()", " upX " + upX);
             if (upX - downX > 100) {

                 // swipe right
             } 

             else if (downX - upX > -100) {

                 // swipe left
             }
             return true;

             }
             return false;
         }
     });
Periostitis answered 18/7, 2014 at 15:30 Comment(5)
it not only works for left, but also for 'up' how to restrict 'up'?Eggbeater
This code up there doesn't take care of Y coordinate. What it basically does is, gets the X coordinate when you first touch on the screen (downX), and gets the X coordinate again when you release your finger (upX). Then it compares these two values to understand which direction your finger moves to. To block "UP" (Meaning +Y Direction), you will need to add downY and upY values, just like X up there. then you can compare these two values, and return false, if you don't that action to be considered.Periostitis
And you can also, change the threshold value, from 100 to something bigger. Then you can ignore small X direction changes, when you move your finger in down-up direction.Periostitis
it should be: if (upX - downX > 100* Constants.density) { } else if (downX - upX > 100 * Constants.density) { } U we're changing the order of the upX - downX and also adding the - to 100 which is incorrect. Right? I mean I took out the - from 100 and it works fine now, doesn't take the Y coordinate, doesn't swipe left at everything I doThrombophlebitis
What the code above does is, finds the difference in X coordinates. Between the one when you touch to screen and the one when you release it, down and up. 100 is the minimum change in the X coordinate. It won't detect the gesture if the difference is less than 100 in X.Periostitis
D
1

Try this will help you....

OnSwipeTouchListener.java:

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

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

    private final class GestureListener extends SimpleOnGestureListener {

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

        @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();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}

Usage:

imageView.setOnTouchListener(new OnSwipeTouchListener() {
    public void onSwipeTop() {
        Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeRight() {
        Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeLeft() {
        Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeBottom() {
        Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
    }

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}
});
Doerrer answered 18/7, 2014 at 15:0 Comment(5)
The same answer you gave is available here: #4139788. You could have suggested the solution through the link or improve it making a better explanation.Sesquialtera
i have syntacs error in setOnTouchListener method can you changed your code sir ?Bracey
@Doerrer how i can write setOnTouchListener method at the moment ?Bracey
this is wrong answer sir. i can not swipe my layoutBracey
l'll edited answer...now try this answer you guys I'm sure it is working right now...Scold

© 2022 - 2024 — McMap. All rights reserved.