Android滚动布局ScrollView滚动到指定的View
本文用于在滚动布局中,操作View到屏幕顶端位置:
Activity生命周期中,onStart, onResume, onCreate都不是真正View真正visible的时间点,真正的visible时间点是onWindowFocusChanged()函数被执行时。 
备注:从onWindowFocusChanged被执行起,用户可以与应用进行交互了,而这之前,对用户的操作需要做一点限制。 
扩展:如果想要做一个Activity一加载完毕,就触发相应操作的话可以在onWindowFocusChanged()函数内操作
如果View还没有visible,使用View的getWidth() 、getHeight() 方法来获取该View的宽和高,返回的值为0。onWindowFocusChanged()内调用getWidth() 、getHeight() 方法可以正常获取宽和高。
1
获取距离
获取View距离父容器的距离:
 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
y = view.getTop(); //顶部距离父容器的Y轴距离
    }
1
2
3
4
5
6
7
方法源码
smoothScrollTo(x,y):
/** 
     * Like {@link #scrollTo}, but scroll smoothly instead of immediately. 
     * 
     * @param x the position where to scroll on the X axis 
     * @param y the position where to scroll on the Y axis 
     */  
    public final void smoothScrollTo(int x, int y) {  
        smoothScrollBy(x - mScrollX, y - mScrollY);  
    }  
1
2
3
4
5
6
7
8
9
10
使用方法
利用scrollView的smoothScrollTo()方法操作View:
scrollView.smoothScrollTo(0,y)
--------------------- 
作者:Hey丶Guesswhat 
来源:CSDN 
原文:https://blog.csdn.net/wang_nannan/article/details/79104863 
版权声明:本文为博主原创文章,转载请附上博文链接!
                    
                
                
            
        
浙公网安备 33010602011771号