Android:在自定义视图组中使用 onTouchEvent 和自定义视图

发布于 2024-12-16 20:12:17 字数 1064 浏览 4 评论 0原文

我有一个称为“Node”的自定义视图,它是称为“NodeGrid”的自定义 ViewGroup 的子级。 “NodeGrid”类更具体地扩展了RelativeLayout。

我的自定义视图类(“Node”)中有以下代码片段:

private boolean isBeingDragged = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
        isBeingDragged = true;
    }
    else if (event.getAction() == MotionEvent.ACTION_UP)
    {
        isBeingDragged = false;
    }
    else if (event.getAction() == MotionEvent.ACTION_MOVE)
    {
        if (isBeingDragged)
        {
            float xPosition = event.getX();
            float yPosition = event.getY();

            //change the x and y position here
        }
    }

    return false;
}

问题:

在此代码中设置断点后,似乎仅针对 MotionEvent 调用 onTouchEvent 。 ACTION_DOWN 情况,但不适用于其他两种情况(“动作向上”或“动作移动”)。有谁知道有什么可能导致这种情况发生吗?

另外(可能相关):

将视图添加到 ViewGroup 的方式重要吗?我注意到除了“addView”之外,还有其他方法可以将子项添加到 ViewGroup,例如“addFocusables”和“addTouchables”。现在我只是使用“addView”将子视图添加到 ViewGroup 中。

I have a custom view which I call "Node" that is a child of a custom ViewGroup called "NodeGrid". The "NodeGrid" class more specifically extends RelativeLayout.

I have the following code snippet in my custom view class ("Node"):

private boolean isBeingDragged = false;

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
        isBeingDragged = true;
    }
    else if (event.getAction() == MotionEvent.ACTION_UP)
    {
        isBeingDragged = false;
    }
    else if (event.getAction() == MotionEvent.ACTION_MOVE)
    {
        if (isBeingDragged)
        {
            float xPosition = event.getX();
            float yPosition = event.getY();

            //change the x and y position here
        }
    }

    return false;
}

The problem:

After having set breakpoints in this code, it seems like onTouchEvent is getting called only for the MotionEvent.ACTION_DOWN case, but not for either of the other two cases ("action up" or "action move"). Does anyone know of anything off hand that could be causing this to happen?

Also (could be related):

Does it matter how the view is added to the ViewGroup? I noticed that in addition to "addView" there are other methods for adding children to a ViewGroup such as "addFocusables" and "addTouchables". Right now I am simply adding the child view to the ViewGroup using "addView".

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

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

发布评论

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

评论(1

扬花落满肩 2024-12-23 20:12:17

来自 SDK 文档

onTouch() - 返回一个布尔值来指示您的侦听器是否使用此事件。重要的是这个事件可以有多个相互跟随的动作。因此,如果您在收到向下操作事件时返回 false,则表明您尚未使用该事件,并且对该事件的后续操作也不感兴趣。因此,您不会被要求执行事件中的任何其他操作,例如手指手势或最终的向上操作事件。

当 ACTION_DOWN 事件被触发时,您需要返回 true 以表明您对与同一事件相关的后续调用感兴趣。

华泰

From the SDK Documentation:

onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

You need to return true when the ACTION_DOWN event is triggered to indicate that you are interested in the subsequent calls relating to that same event.

HTH

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