4

如果在某些特定 ListView 的行中做出这些手势,我需要停止 ListView 以响应用户手势 - 怎么做?目标 ListView 的行视图设置了 onTouchListener,由于 ListView 向上/向下滚动,我无法识别滑动/翻转。因此,如果我将手指向上或向下移动一点 - ListView 会拦截它并沿相应的方向滚动。所以我需要以某种方式来控制它,就像 Y 坐标的变化超过一定数量 - 让 ListView 滚动,如果没有 - 将手势识别为投掷/滑动。OnTouchListener 是

private int SWIPE_MIN_DISTANCE = 1;
private int SWIPE_MAX_OFF_PATH = 300;

final OnTouchListener flingSwipeListener =  new OnTouchListener() {
    float touchX;
    float touchY;
    @Override
    public boolean onTouch(final View view, final MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            touchX = event.getX();
            touchY = event.getY();
        }
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {                   
        if (Math.abs(touchY - event.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
            // right to left swipe
            else if (touchX - event.getX() > SWIPE_MIN_DISTANCE){
                    Log.i("flingSwipe","right to left swipe");
            } 
            // left to right swipe
            else if (event.getX() - touchX > SWIPE_MIN_DISTANCE){
                    Log.i("flingSwipe","left to right swipe");
            }
        }
        return true;

    }
};

这个 onTouchListner 被设置为一个特定的行。我需要在 onTouchListener 识别手势时冻结 ListView,但如果失败,我需要将 MotionEvent 发送到 ListView。

4

2 回答 2

1

如果您需要在特定行上禁用触摸侦听器。比这简单。

在所需的列表行上设置标签。在 getview 列表适配器类中。

例如:

ABCList extends ArrayAdapter<String> {
        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View row = inflater.inflate(R.layout.history_list_item, parent,
                    false);

            if(position == YOUR_DESIRED_ROW)
                          row.setTag("disableThisRow")

            return row;
        }


    }

然后像这样编辑触摸监听器。

 final OnTouchListener flingSwipeListener =  new OnTouchListener() {
    float touchX;
    float touchY;
    @Override
    public boolean onTouch(final View view, final MotionEvent event) {
       String isRow=view.getTag();
       if(isRow.equals("disableThisRow"))return false;

        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            touchX = event.getX();
            touchY = event.getY();
        }
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {                   
        if (Math.abs(touchY - event.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
            // right to left swipe
            else if (touchX - event.getX() > SWIPE_MIN_DISTANCE){
                    Log.i("flingSwipe","right to left swipe");
            } 
            // left to right swipe
            else if (event.getX() - touchX > SWIPE_MIN_DISTANCE){
                    Log.i("flingSwipe","left to right swipe");
            }
        }

        return true;

    }
};

这将禁用对不想要的列表项的触摸..希望这会有所帮助。或者,如果您的手势监听器比这更多,也将与此类似。希望能帮助到你

于 2013-07-09T22:17:45.430 回答
1

如果您想在检测到任何水平滚动(滑动)时禁用 ListView 的垂直滚动,请使用以下解决方案 - 在您的自定义 ListView 类中覆盖 onTouchEvent 并将动作 MotionEvent.ACTION_MOVE 替换为 MotionEvent.ACTION_CANCEL:

public class ListView2 extends ListView
{
    private enum ScrollDirection
    {
        Horizontal,
        Vertical
    }

    private final int touchSlop;

    private float downX = 0;
    private float downY = 0;

    private ScrollDirection scrollDirection;

    public ListView2(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        switch (ev.getActionMasked())
        {
            case MotionEvent.ACTION_MOVE:
                if (scrollDirection == null)
                {
                    if (Math.abs(downX - ev.getX()) > touchSlop) scrollDirection = ScrollDirection.Horizontal;
                    else if (Math.abs(downY - ev.getY()) > touchSlop) scrollDirection = ScrollDirection.Vertical;
                }
                if (scrollDirection == ScrollDirection.Horizontal) ev.setAction(MotionEvent.ACTION_CANCEL);
                break;

            case MotionEvent.ACTION_DOWN:
                scrollDirection = null;
                downX = ev.getX();
                downY = ev.getY();
                break;

        }
        return super.onTouchEvent(ev);
    }
}

当然,您可以使检查逻辑是否禁用垂直滚动更加复杂。

于 2014-10-15T12:53:57.177 回答