2

我有一个AbsoluteLayoutwhich 有一个OnTouchListener. 在这个布局里面有一个小得多的LinearLayout动态定位。OnTouchListener 按预期工作。

现在,当我将 a 添加LongClickListener到我的LinearLayout. OnTouchListener如果触摸击中,这将禁用我,但如果触摸未击中LinearLayout,它仍然会被触发LinearLayout

我的听众:

// listener on parent (AbsoluteLayout)
setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.e("LOOOOGING");
        mLinearLayout.getHitRect(mNoteRect);
        mNoteRect.left += mX;
        mNoteRect.top += mY;
        mNoteRect.right = mNoteRect.left + mLinearLayout.getWidth();
        mNoteRect.bottom = mNoteRect.top + mLinearLayout.getHeight();
        if (mNoteRect.contains((int) event.getX(), (int) event.getY())) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mStartX = (int) event.getX() - mNoteRect.left;
                mStartY = (int) event.getY() - mNoteRect.top;
                return true;
            }
            mX = (int) event.getX() - mStartX;
            mY = (int) event.getY() - mStartY;

            setPadding(mX, mY, 0, 0);
            return true;
        }
        return false;
    }
});

// listener on child (LinearLayout)
mLinearLayout.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // do something...
        return true;
    }
});

如何将注册LinearLayout位置的触摸委托OnLongClickListener给父母?

4

3 回答 3

3

我必须在我的 ontouchlistener 中建立自己的长按行为

private Handler mLongPressHandler = new Handler();

public final Runnable mDoLongPress = new Runnable() {
    public void run() {
        // do something
    }
};

setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mLinearLayout.getHitRect(mNoteRect);
        mNoteRect.left += mX;
        mNoteRect.top += mY;
        mNoteRect.right = mNoteRect.left + mLinearLayout.getWidth();
        mNoteRect.bottom = mNoteRect.top + mLinearLayout.getHeight();
        if (mNoteRect.contains((int) event.getX(), (int) event.getY())) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mStartRawX = (int) event.getX();
                mStartRawY = (int) event.getY();
                mStartX = mStartRawX - mNoteRect.left;
                mStartY = mStartRawY - mNoteRect.top;
                mLongPressHandler.postDelayed(mDoLongPress, 1000);
                return true;
            }
            mX = (int) event.getX() - mStartX;
            mY = (int) event.getY() - mStartY;
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                if ((mStartRawX + 10 < (int) event.getX() || mStartRawX - 10 > (int) event.getX())
                        || (mStartRawY + 10 < (int) event.getY() || mStartRawY - 10 > (int) event.getY())) {
                    mLongPressHandler.removeCallbacks(mDoLongPress);
                }
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                mLongPressHandler.removeCallbacks(mDoLongPress);
            }

            setPadding(mX, mY, 0, 0);
            return true;
        }
        return false;
    }
});
于 2010-09-16T10:41:54.860 回答
1

Maragues 从 onLongClick() 发送 dispatchTouchEvent() 的想法似乎很有希望,但是您必须构建一个事件对象以发送到 dispatchTouchEvent() 以模拟 OnLongClickListener 使用的事件。

我翻转它以拦截父视图中的触摸事件,然后将其转发到子视图。我对父视图进行了子类化,然后添加了这个方法:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // this allows us to catch touch events on the list view if a child button is in the same location, then pass them on to child buttons so they can use them, too
    // we don't have to worry about move events because currently none of the child buttons use them
    int touchX = Math.round(event.getX());
    int touchY = Math.round(event.getY());
    Rect touchRect = new Rect(touchX, touchY, touchX, touchY);
    Log.d("onInterceptTouchEvent", "got touch at " + touchRect);
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            for (View cell : ViewUtils.getSubviews(this)) {
                int cellX = Math.round(cell.getX());
                int cellY = Math.round(cell.getY());
                Rect cellRect = new Rect(cellX, cellY, cellX + cell.getWidth(), cellY + cell.getHeight());
                if (Rect.intersects(touchRect, cellRect)) {
                    for (View button : ViewUtils.getSubviews((ViewGroup) cell)) {
                        if (button instanceof ImageButton) {
                            int buttonX = Math.round(button.getX()) + cellX;
                            int buttonY = Math.round(button.getY()) + cellY;
                            Rect buttonRect = new Rect(buttonX, buttonY, buttonX + button.getWidth(), buttonY + button.getHeight());
                            Log.d("onInterceptTouchEvent", "found button at " + buttonRect);
                            if (Rect.intersects(touchRect, buttonRect)) {
                                Log.d("onInterceptTouchEvent", "forward touch to button");
                                button.dispatchTouchEvent(event);
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            break;
    }
    return true;
}

在我的例子中,父视图是 ListView,子视图是表格单元格内的 ImageButtons。因此,此代码遍历表格单元格,然后遍历每个单元格中的按钮,以找到与触摸位置匹配的按钮,并将触摸转发到该按钮。我的按钮都是使用 OnClickListener 或 OnLongClickListener 的 ImageButton,所以我不转发 ACTION_MOVE 事件,但如果需要,您可以转发。

这是上面使用的 getSubViews() 方法:

public static ArrayList<View> getSubviews(ViewGroup viewGroup) {
    ArrayList<View> subviews = new ArrayList<View>();
    for (int i=0; i<viewGroup.getChildCount(); i++) {
        subviews.add(viewGroup.getChildAt(i));
    }
    return subviews;
}

更新:更简单的版本

上面的代码应该适用于在父视图上接收触摸和在子视图上长按的特定情况。但是我发现这不支持子视图中的常规点击。我认为这与 onInterceptTouchEvent() 处理 ACTION_DOWN 事件的方式不同于处理其他事件的方式有关。此方法的文档非常混乱。

但是,这里有一个更简单的方法,它应该支持父视图或子视图中的各种触摸和单击事件。如上所述,这需要对父视图进行子类化。它还需要直接在该类中设置 onTouch 方法,而不是在另一个类中使用 setOnTouchListener():

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    this.onTouch(this, event); // send the touch to the onTouch method below
    return false; // then let the touch proceed to child buttons
    // return true = this method and this view's onTouch receives events; return false = this method and children's onTouch receive events; remove this method = only children's onTouch receive events
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // work with this touch event here
    return false; // then let the touch continue to other applicable views
    // return true = only this view receives events; return false = this view and other applicable views receive events
}
于 2014-08-13T12:42:34.937 回答
1

你有没有尝试过这样的事情?

   // listener on child (LinearLayout)
mLinearLayout.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        AbsoluteLayout.requestFocus();
        //do something else
        return true;
    }
});

根据我的阅读,触摸相当于获得焦点。(处理 UI 事件

编辑:检查 absoluteLayout 文档,也许这会有所帮助:dispatchTouchEvent(MotionEvent ev)。尝试使用它,听起来像从公共布尔值 onLongClick(View v)启动它可能会有所帮助

将触摸屏运动事件向下传递给目标视图,或者如果它是目标则传递给此视图。

于 2010-09-15T14:49:21.723 回答