Android 如何设置 ScrollView 的起始位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22307239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the starting position of a ScrollView?
提问by RyanCW
I have been trying to set the initial position of a scroll View but have not found a way. Does anyone have any ideas? Also I have a GoogleMaps fragment as a children of the scroll View.
我一直在尝试设置滚动视图的初始位置,但没有找到方法。有没有人有任何想法?此外,我有一个 GoogleMaps 片段作为滚动视图的子项。
Thanks,
谢谢,
Ryan
瑞安
回答by Philipp Jahoda
Yes, that is possible:
是的,这是可能的:
ScrollView.scrollTo(int x, int y);
ScrollView.smoothScrollTo(int x, int y);
ScrollView.smoothScrollBy(int x, int y);
can be used for that. The x and y parameters are the coordinates to scroll to on the horizontal and vertical axis.
可以用于那个。x 和 y 参数是要在水平和垂直轴上滚动到的坐标。
Example in code:
代码示例:
@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);
}
In that example, once the Activity
is started, your ScrollView
will be scrolled down 100 pixels.
在该示例中,一旦Activity
启动,您ScrollView
将向下滚动 100 像素。
You can also try to delaythe scrolling process:
您还可以尝试延迟滚动过程:
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
回答by Vijay
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;
}
});
You can also use scrollView.setScrollY(100)
inside Handler
, but that will be jerky while scrolling.
您也可以使用scrollView.setScrollY(100)
inside Handler
,但滚动时会生涩。
回答by Amir194
After several hours internet searching
经过几个小时的互联网搜索
this worked for me :
这对我有用:
ScrollView.post(new Runnable() {
@Override
public void run() {
//setting position here :
ScrollView.scrollTo(X, Y);
}
});
回答by Selti99
The problem is that the ScrollView doesn't know its size before it been laid out. A solution is to save the scroll position unil onLayouthas been called and then set the scroll position.
问题是 ScrollView 在布局之前不知道它的大小。一个解决方案是保存滚动位置直到onLayout被调用,然后设置滚动位置。
Here is an example (kotlin):
这是一个示例(kotlin):
class MyScrollView constructor(context: Context, attrs: AttributeSet) : ScrollView(context, attrs) {
private var hasStartPositionBeenSet = false
var startPosition = 0
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
super.onLayout(changed, l, t, r, b)
if(!hasStartPositionBeenSet) {
scrollTo(0, startPosition)
hasStartPositionBeenSet = true
}
}
}
class MyActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
/* ... */
findViewById<MyScrollView>(R.id.scrollView).startPosition = scrollPosition
}
}
回答by Selti99
Scrollview sw = (ScrollView) findViewById(R.id.scrollView);
sw.post(new Runnable() {
public void run() {
sw.smoothScrollTo(0, 5000);
}
});
回答by Josef K.
None of the answers worked for me. My ScrollView
is part of a surrounding LinearLayout
. I put the adjustment of the ScrollView
into the onDraw()
method of this LinearLayout
like this:
没有一个答案对我有用。MyScrollView
是周围LinearLayout
. 我把这个的调整ScrollView
放到这个onDraw()
方法里LinearLayout
是这样的:
@Override
protected void onDraw(Canvas canvas){
if(scrollAdjustTries > 2){
return;
}
scrollAdjustTries++;
mScrollView.scrollTo(0, mModel.scrollLine());
}
Very ugly, but working.
非常丑陋,但工作。