获取相对于父ScrollView的视图坐标



之前,我有以下ScrollView和布局。它的滚动直到选定的视图可见代码工作。

<ScrollView>
    <LinearLayout>
        <Info View 1/>
        <Info View 2/>
        <Info View 3/>
    </LinearLayout>
</ScrollView>
private void initScrollView() {
    if (this.selectedInfoView == null) {
        // Nothing to scroll.
        return;
    }
    ScrollView scrollView = (ScrollView)this.getView().findViewById(R.id.scrollView);
    Rect rect = new Rect();
    Rect scrollViewRect = new Rect();
    selectedInfoView.getHitRect(rect);
    scrollView.getDrawingRect(scrollViewRect);
    int dy = rect.bottom - scrollViewRect.bottom;
    if (dy > 0) {
        scrollView.scrollBy(0, dy);
    }
}

注意,getHitRect将返回相对于上一级父节点的坐标。因此,上面的代码可以工作。

然而,当涉及到稍微复杂的情况。以上代码不再工作。

<ScrollView>
    <LinearLayout 0>
        <TextView/>
        <LinearLayout 1>
            <Info View 1/>
            <Info View 2/>
            <Info View 3/>
        </LinearLayout>
        <TextView/>
        <LinearLayout 2>
            <Info View 4/>
            <Info View 5/>
            <Info View 6/>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

在我的代码中,如果我遇到Info View 1 - 3,我需要考虑LinearLayout 0LinearLayout 1getHitRect。当涉及到Info View 4 - 6,我需要考虑到LinearLayout 0LinearLayout 2getHitRect

事情看起来很麻烦。有什么方法可以让我获得一个视图的坐标,相对于最顶部的ScrollView ?

这是当前可行的代码。很麻烦。但在这个时刻它是有效的。我有一些观察。

private void initScrollView() {
    if (this.selectedInfoView == null) {
        // Nothing to scroll.
        return;
    }
    ScrollView scrollView = (ScrollView)this.getView().findViewById(R.id.scrollView);
    LinearLayout linearLayout = null;
    if (this.selectedInfo instanceof Info0) {
        linearLayout = (LinearLayout)this.getView().findViewById(R.id.linearLayout0);
    } else {
        assert(this.selectedInfo instanceof Info1);
        linearLayout = (LinearLayout)this.getView().findViewById(R.id.linearLayout1);
    }
    Rect rect = new Rect();
    Rect linearLayoutRect = new Rect();
    Rect scrollViewRect = new Rect();
    selectedInfoView.getHitRect(rect);
    linearLayout.getHitRect(linearLayoutRect);
    scrollView.getDrawingRect(scrollViewRect);
    // Get coordinate relative to linear layout. See the note below.
    int correct_expected_bottom_y = linearLayoutRect.top + rect.bottom;
    int dy = correct_expected_bottom_y  - scrollViewRect.bottom;
    if (dy > 0) {
        scrollView.scrollBy(0, dy);
    }
}

我还测试了getLocationInWindow, getLocationOnScreengetLocalVisibleRect。没有一个是万无一失的。

(x, y - width, height)

--------------------
|                  | (?, 120) <-- correct expected bottom y
|                  | (0, 146) <-- from getLocationInWindow/ getLocationOnScreen
|                  | (0, 0 - 360, 72) <-- from getLocalVisibleRect
--------------------
|                  | (?, 193) <-- correct expected bottom y
|                  | (0, 219) <-- from getLocationInWindow/ getLocationOnScreen
|                  | (0, 0 - 360, 72) <-- from getLocalVisibleRect
--------------------
|                  | (?, 266) <-- correct expected bottom y
|                  | (0, 292) <-- from getLocationInWindow/ getLocationOnScreen
|                  | (0, 0 - 360, 72) <-- from getLocalVisibleRect
--------------------
|                  | (?, 339) <-- correct expected bottom y
|                  | (0, 365) <-- from getLocationInWindow/ getLocationOnScreen
|                  | (0, 0 - 360, 72) <-- from getLocalVisibleRect
--------------------
|                  | (?, 485) <-- correct expected bottom y
| [not visible]    | (0, 511) <-- from getLocationInWindow/ getLocationOnScreen
|                  | (0, 413 - 360, 485) <-- from getLocalVisibleRect
--------------------

getLocationInWindow

它总是比期望值多26个像素。例如,以第一行26 = 146 - 120为例。我认为这可能是ActionBarStatusBar的高度造成的。

getLocationOnScreen

getLocationInWindow行为相同

getLocalVisibleRect

当行在屏幕上完全不可见时,它只得到与期望值相同的值。不知道为什么。查看最后一行,其中它具有与预期底部y相同的底部y值(485)。

不确定这在2013年是否可用,您现在可以这样做:

val bounds = Rect()
childView.getDrawingRect(bounds) // now bounds has the visible drawing coordinates of the view
parentViewGroup.offsetDescendantRectToMyCoords(childView, bounds) // now bounds has the view's coordinates according to the parentViewGroup

parentViewGroupchildView的父级

最新更新