之前,我有以下的 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>
在我的代码中,如果我遇到信息视图1-3,我需要考虑LinearLayout 0和LinearLayout 1的getHitRect
。当涉及到信息视图4-6时,我需要考虑LinearLayout 0和LinearLayout 2的getHitRect
。
看起来很麻烦。有没有办法让我获取一个视图相对于最上面的ScrollView
的坐标?
ScrollView
是否填满整个屏幕? - user