0

I am starting with android development. I would like to do a calendar activity. I have an activity with it implements OnGestureListener, with methods like onFling(), in order to detect swipe gestures, and change the month of the calendar. This works fine.

After that, my calendar has several cells, one per day. I added to each cell a onclick listener, in order to add a event for this day. When I do this, my gestureListener stops working, and only click events on cell are captured, but swipe gestures dosen't work (onFling() method is never called). What can I do?

This is my code:

public class Calendar extends Activity implements OnGestureListener{ ...

//OnFling method that detects the swipe gesture
@Override
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
   {
       try {
           if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
               return false;
           // right to left swipe
           if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
           }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
           }
           else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show();
           }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show();
           }
       } catch (Exception e) {
        Toast.makeText(this, "exception", Toast.LENGTH_SHORT).show();
       }

         return true;
   }

//This is the method attached to each cell for onclick event
public void showDialog(View v){
    TextView dia = (TextView)v.findViewWithTag("dia");      
    Intent i = new Intent(this, DialogoCalendario.class);
    i.putExtra("dia",(String) dia.getText() );
    i.putExtra("mes",   ((TextView) findViewById(R.id.tbMes)).getText().toString());
    startActivity(i);
}
user1116714
  • 315
  • 2
  • 3
  • 12
  • Show your code and see the following link https://www.google.co.in/search?q=detect+swipe+gestures+android&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – Shruti Mar 09 '12 at 13:12
  • I updated my post with the code – user1116714 Mar 09 '12 at 13:47

1 Answers1

0

Looks like View where you are using GestureListener is loosing the focus when you are working with cell. Just try with calendarCellView.clearFocus(); when you are done with cell.

AndroDev
  • 3,236
  • 8
  • 35
  • 49