Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView Horizontal Fling Gesture

I have a listview that displays entries for a given date. There are buttons above the listview that allow you to increase/decrease the date. Everything works. What I'm looking to do is replace those buttons and have the user swipe right/left to increase/decrease the date.

What's the best way to go about this? I don't care what item is swiped, often there will be no items in the listview for a given date, just as long as it happens on the listview area. I do have click and longclick listeners on the items already.

like image 601
Roger Avatar asked Mar 30 '11 04:03

Roger


Video Answer


1 Answers

Just implement the OnGestureListener.

public class MyListActivity extends ListActivity implements OnGestureListener

Use a GestureDetector

GestureDetector detector = new GestureDetector(this, this);

Pass the touch event of the list to the GestureDetector

listView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View view, MotionEvent e) {
        detector.onTouchEvent(e);
        return false;
    }
});

And finally use the fling method to detect a gesture. You can use the velocity values to detect the direction of the movement.

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {}
like image 82
dbrettschneider Avatar answered Sep 19 '22 19:09

dbrettschneider