如何使用自定义手势监听器专门处理fling和单触

时间:2013-09-19 11:25:18

标签: android android-listview gesture

我有一个ListView,我创建了一个自定义手势监听器来处理fling触摸。我不得不重写onSingleTapConfirmed以让监听器处理项目触摸。不幸的是,如果我触摸某个项目,它会突出显示但不会被推送。

我在活动中使用以下代码:

ListView listView=(ListView)findViewById(android.R.id.list);
final GestureDetector gestureDetector = new GestureDetector(new CustomGestureListener(this));
if (listView != null)
 listView.setOnTouchListener(new View.OnTouchListener()
  {
   @Override
   public boolean onTouch(View v, MotionEvent event)
     {
    if (gestureDetector.onTouchEvent(event)) {
       return false;
         }
       return true;
            }
          });

在CustomGestureListener中:

  @Override
  public boolean onSingleTapConfirmed(MotionEvent event)
  {
    ListView listView=(ListView)((InfoActivity)context).findViewById(android.R.id.list);
    listView.onTouchEvent(event);
    return super.onSingleTapUp(event);
  }

另外,如果我像这样使用滚动,则没有滚动:

@Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
  {        
    return super.onScroll(e1,e2,distanceX,distanceY);
  }

我应该设置什么才能让我的listview项目被点击,滚动并让投掷工作呢?

1 个答案:

答案 0 :(得分:2)

使用它:

ListView lv = new ListView(this);
ArrayAdapter<String> a = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
for (int i = 0; i < 20; i++) {
    a.add("item " + i);
}
SimpleOnGestureListener listener = new SimpleOnGestureListener() {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.d(TAG, "onSingleTapConfirmed ");
        return false;
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.d(TAG, "onFling velocity x " + velocityX);
        Log.d(TAG, "onFling velocity y " + velocityY);
        return false;
    }
};
final GestureDetector gd = new GestureDetector(listener);
OnTouchListener l = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gd.onTouchEvent(event);
    }
};
lv.setOnTouchListener(l);
lv.setAdapter(a);
setContentView(lv);