以前は、次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
1 レベル上の親に対する座標を返すことに注意してください。したがって、上記のコードは機能します。
ただし、やや複雑なケースになると。上記のコードは機能しなくなりました。
<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 0とLinearLayout 1を考慮する必要がありますgetHitRect
。Info View 4-6 に関して言えば、 LinearLayout 0とLinearLayout 2について考慮する必要がありますgetHitRect
。
物事は面倒に見えます。一番上を基準にしてビューの座標を取得する方法はありますScrollView
か?