获取scrollview中子view坐标

   private void scrollToPos(final View view, final View selectViewGroup, final View details) {
      mMyScrollView.post(new Runnable() {
         @Override
         public void run() {
            UtilsLog.d(getHeight(mMyScrollView));
            /*int[] location = new int[2];
            view.getLocationInWindow(location);
            UtilsLog.d(location[0]);
            UtilsLog.d(location[1]);

            UtilsLog.d(getHeight(view));

            UtilsLog.d(mMyScrollView.getMeasuredHeight());
            int offset = 0;
//          int offset = location[1] - mMyScrollView.getMeasuredHeight();
//          if (offset < 0) {
//             offset = 0;
//          }
            offset = location[1] - getHeight(view);
            UtilsLog.d(offset);
            mMyScrollView.smoothScrollTo(0, offset);*/
            int[] location = new int[2];
            selectViewGroup.getLocationInWindow(location);
            UtilsLog.d(location[1]);

            Rect gropurect = new Rect();
            Rect textviewrect = new Rect();
            Rect detailsrect = new Rect();
            Rect scrollViewRect = new Rect();

            selectViewGroup.getHitRect(gropurect);
            view.getHitRect(textviewrect);
            details.getHitRect(detailsrect);
            mMyScrollView.getDrawingRect(scrollViewRect);

            UtilsLog.d(gropurect);
            UtilsLog.d(textviewrect);
            UtilsLog.d(detailsrect);
            UtilsLog.d(scrollViewRect);

            // Get coordinate relative to linear layout. See the note below.
            int correct_expected_bottom_y = textviewrect.top + gropurect.bottom;

            //int dy = correct_expected_bottom_y  - scrollViewRect.bottom;
            int dy = detailsrect.top - location[1];
            UtilsLog.e(dy);
            if (dy > 0) {
               //mMyScrollView.scrollBy(0, dy);
               mMyScrollView.smoothScrollTo(0, dy);
            }
         }
      });
   }

   public int getHeight(View view) {// 获得某组件的高度
      int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
      int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
      view.measure(w, h);
      return view.getMeasuredHeight();
   }

http://stackoverflow.com/questions/15540099/getting-coordinate-of-view-with-relative-to-parent-scrollview
<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>

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值