ListView Horizontal Fling Gesture

有些话、适合烂在心里 提交于 2020-02-20 08:50:13

问题


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.


回答1:


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) {}


来源:https://stackoverflow.com/questions/5481814/listview-horizontal-fling-gesture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!