Rya*_*nCW 13 android android-scrollview
我一直试图设置滚动视图的初始位置,但没有找到方法.有没有人有任何想法?此外,我还有一个GoogleMaps片段作为滚动视图的子项.
谢谢,
瑞安
Phi*_*oda 21
是的,这是可能的:
ScrollView.scrollTo(int x, int y);
ScrollView.smoothScrollTo(int x, int y);
ScrollView.smoothScrollBy(int x, int y);
Run Code Online (Sandbox Code Playgroud)
可以用于此.x和y参数是在水平和垂直轴上滚动到的坐标.
代码示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
sv.scrollTo(0, 100);
}
Run Code Online (Sandbox Code Playgroud)
在该示例中,一旦Activity启动,您ScrollView将向下滚动100像素.
您还可以尝试延迟滚动过程:
final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
sv.scrollTo(0, 100);
}
}, 250); // 250 ms delay
Run Code Online (Sandbox Code Playgroud)
The accepted answer is not working for me. There is no direct way to set the initial position of a scroll view.
However, you can set the initial position before drawing the scroll view, like this:
rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
scrollView.getViewTreeObserver().removeOnPreDrawListener(this);
scrollView.setScrollY(100);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
You can also use scrollView.setScrollY(100) inside Handler, but that will be jerky while scrolling.
| 归档时间: |
|
| 查看次数: |
30885 次 |
| 最近记录: |