gpt4 book ai didi

android - 如何在 subview 和父 View 组触摸事件之间变化

转载 作者:IT老高 更新时间:2023-10-28 23:30:53 24 4
gpt4 key购买 nike

我决定发布这个问题和答案以回应 to this comment对于这个问题:
How to handle click in the child Views, and touch in the parent ViewGroups?

我会把评论贴在这里:

Suppose I want to override the touch events only for handling some of the children, what can I do inside this function to have it working ? I mean, for some children it would work as usual, and for some, the parent-view will decide if they will get the touch events or not.

所以问题是这样的:如何防止父 onTouchEvent() 覆盖某些子元素的 onTouchEvent(),同时让它覆盖其他子元素的 onTouchEvent() ?

最佳答案

  1. 嵌套 View 组的 onTouchEvents() 可以由 boolean onInterceptTouchEvent 管理.

OnInterceptTouchEvent 的默认值为 false。

parent 的onTouchEvent 在 child 之前收到。如果 OnInterceptTouchEvent 返回 false,它会将运动事件沿链向下发送到子级的 OnTouchEvent 处理程序。如果它返回 true,则父级将处理触摸事件。

但是,在某些情况下,我们可能希望某些子元素管理 OnTouchEvent,而某些子元素由父 View (或可能是父 View 的父 View )管理。

这可以通过多种方式进行管理。

  1. 保护子元素免受父元素 OnInterceptTouchEvent 影响的一种方法是实现 requestDisallowInterceptTouchEvent .

public void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

如果元素启用了事件处理程序,这将阻止任何父 View 管理该元素的 OnTouchEvent

  1. 如果 OnInterceptTouchEvent 为 false,则将评估子元素的 OnTouchEvent。如果您在子元素中有处理各种触摸事件的方法,则任何禁用的相关事件处理程序都会将 OnTouchEvent 返回给父元素。

这个答案:
https://stackoverflow.com/a/13540006/3956566很好地可视化了触摸事件的传播方式:
parent -> child|parent -> child|parent -> subview 。

  1. 另一种方法是从 OnInterceptTouchEvent 为父级返回不同的值。

这个例子取自 Managing Touch Events in a ViewGroup并演示了如何在用户滚动时拦截 child 的OnTouchEvent

4a。

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
/*
* This method JUST determines whether we want to intercept the motion.
* If we return true, onTouchEvent will be called and we do the actual
* scrolling there.
*/


final int action = MotionEventCompat.getActionMasked(ev);

// Always handle the case of the touch gesture being complete.
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
// Release the scroll.
mIsScrolling = false;
return false; // Do not intercept touch event, let the child handle it
}

switch (action) {
case MotionEvent.ACTION_MOVE: {
if (mIsScrolling) {
// We're currently scrolling, so yes, intercept the
// touch event!
return true;
}

// If the user has dragged her finger horizontally more than
// the touch slop, start the scroll

// left as an exercise for the reader
final int xDiff = calculateDistanceX(ev);

// Touch slop should be calculated using ViewConfiguration
// constants.
if (xDiff > mTouchSlop) {
// Start scrolling!
mIsScrolling = true;
return true;
}
break;
}
...
}

// In general, we don't want to intercept touch events. They should be
// handled by the child view.
return false;
}

编辑:回答评论。
这是来自同一链接的一些代码,展示了如何在元素周围创建矩形的参数:
4b。

// The hit rectangle for the ImageButton
myButton.getHitRect(delegateArea);

// Extend the touch area of the ImageButton beyond its bounds
// on the right and bottom.
delegateArea.right += 100;
delegateArea.bottom += 100;

// Instantiate a TouchDelegate.
// "delegateArea" is the bounds in local coordinates of
// the containing view to be mapped to the delegate view.
// "myButton" is the child view that should receive motion
// events.
TouchDelegate touchDelegate = new TouchDelegate(delegateArea, myButton);

// Sets the TouchDelegate on the parent view, such that touches
// within the touch delegate bounds are routed to the child.
if (View.class.isInstance(myButton.getParent())) {
((View) myButton.getParent()).setTouchDelegate(touchDelegate);
}

关于android - 如何在 subview 和父 View 组触摸事件之间变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com