相应链接:
https://blog.csdn.net/nihaoqiulinhe/article/details/51489524
正如这篇博客介绍的那样,在Android开发中遇到一屏显示不下所有的内容,大家肯定会想到使用scroollView。
可是由于某些需求,会要求在最开始显示scrollview的时候就定位到某一处。但用在我的项目中(PAD端Dialog)总是定位的不是很"准确"。
大部分是下面这种写法
1、先定义一个runnable接口
2、用一个handler来把这个runnable对象post出来,最好来个延时
其结果是总是定位"不太准",尤其item有一定高度时。
// 动画显示稍后放上
自己的实现方式
重写 onWindowFocusChanged 方法
/**
if the window gains/losses focus, it will be triggered.
@params hasFocus 是否获得焦点
**/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// do anything you want here,for instance
try{
LinearLayout view = 得到具体的view对象
scrollView.smoothScrollTo(0,view.getTop()-10);
........
}catch(NullPointerException exception){
}
}
实现效果如下
// 晚点放上
代码 https://download.csdn.net/download/hello_xiaozhou/11003387.