我有一个不在“ ScrollView”根目录下的视图。如果我将scrollTo(0, view.getTop())
设置为无效,它只会滚动一点,我想是因为视图不在ScrollView
的根容器中。
我的视图树位于以下位置:
ScrollView
- LinearLayout
- CardView
- RelativeLayout
- LinearLayout
- MyView
有什么想法可以将滚动视图精确地移动到MyView.getTop()
位置吗?
答案 0 :(得分:0)
尝试
mainScrollView.fullScroll(ScrollView.FOCUS_UP);
应该可以。
答案 1 :(得分:0)
我使用以下代码在特定位置显示popupWindow,检查其是否适合您的情况
val location = locateView(view) // here view will be the view to which you want to scroll
scrollview.scrollTo(0, location.top)
和locateview函数是
fun locateView(v: View?): Rect? {
var loc_int = IntArray(2)
if (v == null) return null;
try {
v.getLocationOnScreen(loc_int)
} catch (e : NullPointerException) {
return null
}
val location = Rect()
location.left = loc_int[0]
location.top = loc_int[1]
location.right = location.left + v.width
location.bottom = location.top + v.height
return location
}