简体   繁体   English

如何将scrollView滚动到内部View的位置

[英]How to scroll scrollView to position of inner View

I have a view which is not at the root level of the "ScrollView". 我有一个不在“ ScrollView”根目录下的视图。 If i set scrollTo(0, view.getTop()) it won't work, it'll scroll just a bit, I am guessing because the view is not in the root container of the ScrollView . 如果我将scrollTo(0, view.getTop())为无效,它将滚动scrollTo(0, view.getTop()) ,我想是因为该视图不在ScrollView的根容器中。

My view tree is somewhere along the lines of: 我的视图树位于以下位置:

ScrollView 
   - LinearLayout
        - CardView
           - RelativeLayout
              - LinearLayout
                   - MyView

Any ideas how I can move the scrollview precisely to MyView.getTop() position ? 有什么想法可以将scrollview精确地移动到MyView.getTop()位置吗?

Try 尝试

mainScrollView.fullScroll(ScrollView.FOCUS_UP);

It Should work. 它应该工作。

I used below code for showing a popupWindow at specific location , check if its working in your case 我使用下面的代码在特定位置显示popupWindow,检查它是否适合您的情况

 val location = locateView(view)  // here view will be the view to which you want to scroll
 scrollview.scrollTo(0, location.top)

and locateview function is 和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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM