ListView水平投掷手势

Rog*_*ger 5 android listview

我有一个列表视图,显示给定日期的条目.列表视图上方有按钮,可用于增加/减少日期.一切正常.我要做的是更换这些按钮,让用户向右/向左滑动以增加/减少日期.

最好的方法是什么?我不关心什么项目被刷过,通常列表视图中没有项目给定日期,只要它发生在列表视图区域.我已经对这些项目进行了click和longclick监听.

dbr*_*der 13

只需实现OnGestureListener.

public class MyListActivity extends ListActivity implements OnGestureListener
Run Code Online (Sandbox Code Playgroud)

使用GestureDetector

GestureDetector detector = new GestureDetector(this, this);
Run Code Online (Sandbox Code Playgroud)

将列表的触摸事件传递给GestureDetector

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

最后使用fling方法检测手势.您可以使用力度值来检测运动方向.

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,它的工作原理.是否有标准数字代表投掷?我正在使用if(velocityX> 150)......从右到左投掷.它有效,但有点主观. (3认同)