Android 停止发送 onTouch 事件

发布于 2024-12-02 19:05:09 字数 3476 浏览 1 评论 0原文

我有一个自定义视图,我希望为其处理触摸事件。特别是,在视图上拖动手指应该会导致其内容滚动。为此,我实现了以下 onTouchListener:

private OnTouchListener graphOnTouchListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("onTouch called");
        System.out.println("view width: " + v.getWidth());
        System.out.println("view height: " + v.getHeight());
        System.out.println("view: " + v);

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                // First finger down
                System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
                startTouch(event.getX());
                break;
            case MotionEvent.ACTION_UP:
                // Last finger to be removed
                System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                // A finger moves
                System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
                moveTouch(event.getX());
                break;
            default:
                break;
        }  

        // The event has been handled, so return true
        return true;
    }
};

这将按预期工作,除非用户在视图上向上或向下移动手指。此时,触摸事件停止发送(LogCat 中缺少输出即可证明)。但是,使用 adb shell getevent 仍然显示触摸移动生成的事件。

谁能建议我如何纠正这个问题?

根据要求,布局 XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">
    <FrameLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:padding="10dip">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:orientation="vertical">
                <com.graphlib.ShaderAreaGraphView
                    android:layout_width="fill_parent" android:layout_height="200dip"
                    android:id="@+id/activity_power_realtime" android:background="#000000" />
                <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center">
                    <Button android:text="&lt;&lt;" android:id="@+id/goBackwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                    <Button android:text="Zoom out" android:id="@+id/zoomOutButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                    <Button android:text="Zoom in" android:id="@+id/zoomInButton" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
                    <Button android:text="&gt;&gt;" android:id="@+id/goForwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </FrameLayout>
</LinearLayout>

I have a custom view which I wish to handle touch events for. In particular, dragging a finger over the view should cause its contents to scroll. To do this, I have implemented the following onTouchListener:

private OnTouchListener graphOnTouchListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("onTouch called");
        System.out.println("view width: " + v.getWidth());
        System.out.println("view height: " + v.getHeight());
        System.out.println("view: " + v);

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                // First finger down
                System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
                startTouch(event.getX());
                break;
            case MotionEvent.ACTION_UP:
                // Last finger to be removed
                System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                // A finger moves
                System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
                moveTouch(event.getX());
                break;
            default:
                break;
        }  

        // The event has been handled, so return true
        return true;
    }
};

This works as expected, unless the user moves their finger up or down while on the view. At this point, touch events stop being sent (as evidenced by a lack of output in LogCat). However, using adb shell getevent still shows events being generated by touch movement.

Can anyone suggest how I can rectify this?

As requested, the layout XML looks like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">
    <FrameLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:padding="10dip">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:orientation="vertical">
                <com.graphlib.ShaderAreaGraphView
                    android:layout_width="fill_parent" android:layout_height="200dip"
                    android:id="@+id/activity_power_realtime" android:background="#000000" />
                <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center">
                    <Button android:text="<<" android:id="@+id/goBackwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                    <Button android:text="Zoom out" android:id="@+id/zoomOutButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                    <Button android:text="Zoom in" android:id="@+id/zoomInButton" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
                    <Button android:text=">>" android:id="@+id/goForwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </FrameLayout>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你丑哭了我 2024-12-09 19:05:09

您需要在每种情况下编写break语句。否则每个案件都会被执行。

        case MotionEvent.ACTION_DOWN:
            // First finger down
            System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
            startTouch(event.getX());
            break;
        case MotionEvent.ACTION_UP:
            // Last finger to be removed
            System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
            break;
        case MotionEvent.ACTION_MOVE:
            // A finger moves
            System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
            moveTouch(event.getX());
            break;

或者你可以放入 if ... else if 条件

you need write the break statement in each case. otherwise every case are executed.

        case MotionEvent.ACTION_DOWN:
            // First finger down
            System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
            startTouch(event.getX());
            break;
        case MotionEvent.ACTION_UP:
            // Last finger to be removed
            System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
            break;
        case MotionEvent.ACTION_MOVE:
            // A finger moves
            System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
            moveTouch(event.getX());
            break;

or you can put into the if ... else if condition

机场等船 2024-12-09 19:05:09

事实证明,问题在于视图包含在 ScrollView 中。删除 ScrollView 可以解决该问题。

It turns out the issue is that the view is contained in a ScrollView. Removing the ScrollView fixes the problem.

孤云独去闲 2024-12-09 19:05:09

解决此问题的一个简单方法是创建对滚动视图的引用,例如 myScrollView = (ScrollView) findViewById(R.id.myScrollView)。然后在onTouchEvent,ACTION_DOWN状态:

myScrollView.requestDisallowInterceptTouchEvent(true);

在 ACTION_UP 中

myScrollView.requestDisallowInterceptTouchEvent(false);

An easy way to solve this is to create a reference to the scroll view e.g. myScrollView = (ScrollView) findViewById(R.id.myScrollView). Then in onTouchEvent, ACTION_DOWN state:

myScrollView.requestDisallowInterceptTouchEvent(true);

And in ACTION_UP

myScrollView.requestDisallowInterceptTouchEvent(false);

谜兔 2024-12-09 19:05:09

在开关的底部,尝试添加

default:
    break;

可能不是解决办法,但这样做是一个很好的做法。

At the bottom of your switch, try adding

default:
    break;

Might not be the fix, but it's good practice to have that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文