0

我使用 Flipper 作为父级,使用 Listview 作为子级。我的问题是列表视图中项目的翻转和单击。当我翻到下一页(通过从右向左拖动)时,我不小心单击了一个列表项。

当我已经做出翻转手势时,如何禁用列表视图的 onClick?

代码:

脚蹼 Ontouch:

public boolean dispatchTouchEvent(MotionEvent touchevent) {
        super.dispatchTouchEvent(touchevent);
        switch (touchevent.getAction()) {
        case MotionEvent.ACTION_DOWN: {
        lastX = touchevent.getX();
        break;
    }
    case MotionEvent.ACTION_UP: {
        float currentX = touchevent.getX();
        if (lastX - 100 > currentX) {
            if (result_pageNum < max_pageNum) {
                result_pageNum++;
                if (vf.getDisplayedChild() == 0) {
                    listView[1].setClickable(false);
                    setListView(1);
                } else {
                    listView[0].setClickable(false);
                    setListView(0);
                }
                vf.setInAnimation(this, R.anim.in_from_right);
                vf.setOutAnimation(this, R.anim.out_to_left);
                vf.showNext();
            }
        } else if (lastX + 100 < currentX) {
            if (result_pageNum > 0) {
                result_pageNum--;
                if (vf.getDisplayedChild() == 1) {
                    listView[0].setClickable(false);
                    setListView(0);
                } else {
                    listView[1].setClickable(false);
                    setListView(1);
                }
                vf.setInAnimation(this, R.anim.in_from_left);
                vf.setOutAnimation(this, R.anim.out_to_right);
                vf.showPrevious();
            }
        }
        break;
    }
    }
    return false;
}

listView onClick:

private void listView_onClick() {
    for (int i = 0; i < listView.length; i++) {
        listView[i].setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text

Toast.maketext(this,"Working!",Toast.LENGTH_LONG).show();

            }
        });
    }

}
4

2 回答 2

0

您不应该真正dispatchTouchEvent(MotionEvent)用于此目的。

相反,您应该使用onInterceptTouchEvent(MotionEvent)and onTouchEvent(MotionEvent)

这些方法之间的关系记录在方法的 Javadoc 中,但作为总结:

onInterceptTouchEvent(MotionEvent)决定是否ViewGroup从它的任何子视图中截取用户触摸事件。例如,您ViewFlipper决定 ListView 是否接收事件。

onTouchEvent(MotionEvent)是您ViewFlipper可以实际对其触摸事件做出反应的地方。

于 2013-01-10T10:09:21.720 回答
0

在您的 dispatchTouchEvent 方法中,当检测到一个投掷手势时(在 ACTION_UP 事件下),尝试将true作为布尔值返回,而不是每次都返回 false。当根据您的运动计算未检测到投掷手势时,则仅返回false

于 2013-01-10T09:33:07.373 回答