Listview阻止手势

时间:2014-03-09 08:22:48

标签: android

public class Main extends Activity {
GestureDetectorCompat gdc;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    list = (ListView) findViewById(R.id.listview);
    gdc = new GestureDetectorCompat(this, new GesturesClass());
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    gdc.onTouchEvent(event);
    return super.onTouchEvent(event);
}


class GesturesClass extends SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        //Bring left activity
        if((e2.getX() - e1.getX()) > 100){
            Intent intent = new Intent(Main.this, Finished.class);
            startActivity(intent);
            Toast.makeText(Main.this, "Bring left actitivty.", Toast.LENGTH_LONG).show();
        }else if((Math.abs(e1.getX() - e2.getX())) > 100){
            Toast.makeText(Main.this, "Bring right actitivty.", Toast.LENGTH_LONG).show();              
            Intent intent = new Intent(Main.this, Unfinished.class);
            startActivity(intent);
        }
        return super.onFling(e1, e2, velocityX, velocityY);
    }
}

好的所以我已经实现了手势,其中包括向左和向右投掷以开始每侧的新活动。一切都应该完美。当我设置列表视图项时,listview开始阻止我的手势。我搜索了一下dispatchTouchEvent,它给你优先触摸事件,我无法理解如何在我的情况下实现它。请帮忙。

1 个答案:

答案 0 :(得分:3)

我认为应该是这样的:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gdc.onTouchEvent(event)) {
      return true;
    }
    return false;
}

编辑:

onTouchListener添加自定义ListView,然后return gdc.onTouchEvent(event);

listView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gdc.onTouchEvent(event);
    }
});