我正在使用以下代码创建视图的触摸委托。问题在于,我想要增加可点击区域的视图位于非常狭窄的LinearLayout内部。因此,下面的代码可以增加我的视图的可点击区域,但仅在我狭窄的LinearLayout范围内。我想将myView.getParent().getParent()(RelativeLayout)而不是父级传递给此函数,因为它有更大的可点击区域空间。但是,touchRect将指向错误的位置,我的TouchDelegate将被错误地定位。原因是:delegate.getHitRect(touchRect); 返回相对于父级而不是父级(或父级的父级)的位置。
有什么建议吗?
public static Runnable getTouchDelegateAction(final View delegate, final View parent, final int topPadding, final int bottomPadding, final int leftPadding,
final int rightPadding) {
return new Runnable() {
@Override
public void run() {
// Construct a new Rectangle and let the Delegate set its values
Rect touchRect = new Rect();
delegate.getHitRect(touchRect);
// Modify the dimensions of the Rectangle
// Padding values below zero are replaced by zeros
touchRect.top -= Math.max(0, topPadding);
touchRect.bottom += Math.max(0, bottomPadding);
touchRect.left -= Math.max(0, leftPadding);
touchRect.right += Math.max(0, rightPadding);
// Now we are going to construct the TouchDelegate
TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate);
// And set it on the parent
parent.setTouchDelegate(touchDelegate);
}
};
}
有什么建议吗?