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
getLocationInWindow
,getLocationOnScreen
andgetLocalVisibleRect
. 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
, orStatusBar
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.