Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting coordinate of view with relative to parent ScrollView

Tags:

android

Previously, I have the following ScrollView and layout. Its scroll till selected view visible code works.

<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);
    }
}

Note, getHitRect will return the coordinate respect to one level up parent. So, the above code will work.

However, when comes to slightly complicated case. The above code no longer work.

<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>

In my code, if I encounter Info View 1 - 3, I need to take consideration into LinearLayout 0 and LinearLayout 1's getHitRect. When comes to Info View 4 - 6, I need to take consideration into LinearLayout 0 and LinearLayout 2's getHitRect.

Things look cumbersome. Is there any way for me to obtain the coordinate of a view, relative to top most ScrollView?

like image 753
Cheok Yan Cheng Avatar asked Mar 21 '13 05:03

Cheok Yan Cheng


1 Answers

This is the currently workable code. Is cumbersome. But it works at this moment. I have a few observations.

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);
    }
}

I also tested with getLocationInWindow, getLocationOnScreen and getLocalVisibleRect. None of them fool proof.

(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

It always have 26 more pixels than the expected value. For example, take the first row, 26 = 146 - 120. I think that might be contributed by ActionBar, or StatusBar height.

getLocationOnScreen

Same behavior as getLocationInWindow

getLocalVisibleRect

It only get same value as expected value, when the row is completely not visible on screen. Not sure why. Look at the last row, where it is having same bottom y value (485) as expected bottom y.

like image 189
Cheok Yan Cheng Avatar answered Sep 28 '22 07:09

Cheok Yan Cheng