4

我正在使用以下代码来创建视图的触摸委托。问题是我想要增加的可点击区域的视图在非常窄的 LinearLayout 内。所以下面的代码可以增加我的视图的可点击区域,但仅限于我狭窄的 LinearLayout 的范围内。我想传递给这个函数的不是父函数,而是 myView (myView.getParent().getParent()) 的祖父函数。这是一个相对布局,有更大的可点击区域空间。但是随后 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);
        }
    };
}

有什么建议么?

4

2 回答 2

4

如果有人有类似的问题,那么这是我找到的解决方案。不同之处在于可以设置标志bindToGrandparent,以便将委托给可点击视图的祖父母。

public static Runnable getTouchDelegateAction(final View delegate, final boolean bindToGrandparent, final int topPadding,
        final int bottomPadding, final int leftPadding, final int rightPadding) {
    return new Runnable() {
        @Override
        public void run() {
            View parent = (View) delegate.getParent();
            View grandparent = (View) parent.getParent();

            // Construct a new Rectangle and let the Delegate set its values
            Rect touchRect = new Rect();
            delegate.getHitRect(touchRect);

            if (bindToGrandparent) {
                touchRect.top += parent.getTop();
                touchRect.left += parent.getLeft();
                touchRect.bottom += parent.getTop();
                touchRect.right += parent.getLeft();
            }

            // 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
            if (bindToGrandparent) {
                grandparent.setTouchDelegate(touchDelegate);
            } else {
                parent.setTouchDelegate(touchDelegate);
            }
        }
    };
}

它应该按如下方式使用:

grandparentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, true, 30, 30, 30, 30));

重要的是应该将程序发布到视图的祖父母。

或者:

parentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, false, 30, 30, 30, 30));
于 2013-08-08T10:10:03.500 回答
1

如果您需要创建与祖父母甚至祖父母相关的视图的触摸委托,您可以在代码中添加一行:

((ViewGroup)parent).offsetDescendantRectToMyCoords(delegate, 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() {
            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);

            // calculates the relative coordinates to the parent
            ((ViewGroup) parent).offsetDescendantRectToMyCoords(delegate, touchRect);
            parent.setTouchDelegate(new TouchDelegate(touchRect, delegate));
        }
    };
}

在这里你可以找到官方文档。

于 2017-02-27T10:01:53.970 回答