1

In my code right to left swipe works properly but left to right swipe not working. I dont know whats the reason.

Code-

public class Types extends Activity{

    RelativeLayout layout1;
    int[] backgroundResId;
    int currentIndex=0;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type);
        backgroundResId=new int[]{R.drawable.back,R.drawable.bg,R.drawable.frame1};
        layout1 = (RelativeLayout) findViewById(R.id.layout1);
        changeBackground();
        ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
        layout1.setOnTouchListener(activitySwipeDetector);

    }


    private void changeBackground(){
         layout1.setBackgroundResource(backgroundResId[currentIndex]);
    }

    public class ActivitySwipeDetector implements View.OnTouchListener {

        static final String logTag = "ActivitySwipeDetector";
        static final int MIN_DISTANCE = 100;
        private float downX, upX;
        private Activity activity;

        public ActivitySwipeDetector(Activity activity){
            this.activity = activity;
        }

        public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            currentIndex++;
            if(currentIndex<backgroundResId.length){
                 changeBackground();
            }
        }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(currentIndex>0){
                changeBackground();
           }
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    upX = event.getX();

                    float deltaX = downX - upX;

                    // swipe horizontal?
                    if(Math.abs(deltaX) > MIN_DISTANCE){
                        // left or right
                        if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
                        if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
                    }
                    else {
                            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                            return false; // We don't consume the event
                    }


                    return true;
                }
            }
            return false;
        }

        }

On this line private Activity activity; I am getting warning. Why?

John R
  • 2,078
  • 8
  • 35
  • 58

2 Answers2

1

this is a sample to work with SimpleOnGestureListener

GestureDetector gestureDetector = new GestureDetector(this, new MyGestureDetector());
layout1 = (RelativeLayout) findViewById(R.id.layout1);  // main Layout

then

layout1.setOnTouchListener(new View.OnTouchListener() {
       public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

and MyGestureDetector:

class MyGestureDetector extends SimpleOnGestureListener {
    SWIPE_THRESHOLD_VELOCITY = 200;
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {


        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            // i think this is swipe to right

        }

        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               //and i think this is swipe to left

        }

        return false;
    }

    // It is necessary to return true from onDown for the onFling event to
    // register
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • I don't think so your way be correct, because when you put your finger on screen onDown called and while your finger is on Screen your `downX` has been updated, if you want use your way I think you can handled with one boolean value to catch `downX` just one time, but really i don't know what is the problem – Shayan Pourvatan Jan 14 '14 at 10:51
  • id you test `SimpleOnGestureListener`? did this worked? – Shayan Pourvatan Jan 14 '14 at 10:52
  • Ok. Is it complete code for swiping from left to right and right to left? – John R Jan 14 '14 at 10:58
  • yes, but i don't know which is for left to right and witch for right to left, i comment for you – Shayan Pourvatan Jan 14 '14 at 10:59
1

You forgot to minus currentIndex

 public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(currentIndex>0){
                currentIndex--;
                changeBackground();
           }
        }
Groco
  • 1,301
  • 15
  • 19