Achuthan M
Achuthan M

Reputation: 187

Using OnTouchListener to implement a slider structure in android

I am working on a project which has a double slider structure in it.I am using The ontouchlistener to cause the sliding motion.There are two sliders ,one is the left slider and another is the right slider.The sliding gesture of the right slider all of a sudden stopped working.I havent made changes to the main class.When the right slider opens a webservice is called to populate a listview in it.

Upvotes: 0

Views: 491

Answers (2)

Arun Salaria
Arun Salaria

Reputation: 984

       public class Swipe_Activity extends Activity {
      LinearLayout swipe_layout;
         @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
        swipe_layout=(LinearLayout) findViewById(R.id.layout_swipe);
        swipe_layout.setOnTouchListener( new OnSwipeTouchListener(this));
        }
       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 = 60;
        private static final int SWIPE_VELOCITY_THRESHOLD = 60;

        @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() {
        Log.i("Post", "right");
    }

    public void onSwipeLeft() {
        Log.i("Post", "left");
        Intent intent=new Intent(Swipe_Activity.this,Questions_Answers_Activity.class);
        startActivity(intent);
    }

    public void onSwipeTop() {
        Log.i("Post", "Top");
    }

    public void onSwipeBottom() {
        Log.i("Post", "Bottom");
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureDetector.onTouchEvent(event);
    }


}

Upvotes: 2

Akash Moradiya
Akash Moradiya

Reputation: 3322

Change your code like,

In GestureListener

@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();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }

Upvotes: 0

Related Questions