与ScrollView .scrollTo not working? Saving ScrollView position on rotation
相同的问题我在onCreate中向scrollView动态添加项目。添加完所有项目后,我尝试以下操作:
// no effect
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
// no effect
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.post(new Runnable() {
public void run(){
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.scrollTo(0, 0);
}
});
// works like a charm
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.postDelayed(new Runnable() {
public void run(){
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
mainScroll.scrollTo(0, 0);
}
}, 30);
我的结论是有一些像'DOM-ready'这样的事件?有没有回调?
答案 0 :(得分:15)
您不需要扩展ScrollView并根据David Daudelin为this question提供的答案创建自己的。
获取ViewTreeObserver
的{{1}}并向ScrollView
添加OnGlobalLayoutListener
。然后从ViewTreeObserver
的{{1}}方法调用ScrollView.scrollTo(x,y)
方法。代码:
onGlobalLayout()
答案 1 :(得分:3)
这对我有用,将scrollView.scrollTo()
替换为
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.scrollTo(0, ScrollViewYPosition);//0 is x position
}
});
Vasily Kabunov在
中的回答得到了帮助ScrollView .scrollTo not working? Saving ScrollView position on rotation
答案 2 :(得分:1)
当您对ScrollView进行一些更改时,它需要一段时间才能将布局更改复制到显示列表,并通知ScrollView它确实允许滚动到某个位置。
我已经设法通过使用我自己的ScrollView扩展ScrollView
并添加一个根据需要添加OnGlobalLayoutListener
(由MH建议)并稍后滚动的方法。它比将它应用于您需要它的每个案例都要自动化一些(但是您需要使用新的ScrollView
)。无论如何,这是相关的代码:
public class ZScrollView extends ScrollView {
// Properties
private int desiredScrollX = -1;
private int desiredScrollY = -1;
private OnGlobalLayoutListener gol;
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
public ZScrollView(Context __context) {
super(__context);
}
public ZScrollView(Context __context, AttributeSet __attrs) {
super(__context, __attrs);
}
public ZScrollView(Context __context, AttributeSet __attrs, int __defStyle) {
super(__context, __attrs, __defStyle);
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
public void scrollToWithGuarantees(int __x, int __y) {
// REALLY Scrolls to a position
// When adding items to a scrollView, you can't immediately scroll to it - it takes a while
// for the new addition to cycle back and update the scrollView's max scroll... so we have
// to wait and re-set as necessary
scrollTo(__x, __y);
desiredScrollX = -1;
desiredScrollY = -1;
if (getScrollX() != __x || getScrollY() != __y) {
// Didn't scroll properly: will create an event to try scrolling again later
if (getScrollX() != __x) desiredScrollX = __x;
if (getScrollY() != __y) desiredScrollY = __y;
if (gol == null) {
gol = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int nx = desiredScrollX == -1 ? getScrollX() : desiredScrollX;
int ny = desiredScrollY == -1 ? getScrollY() : desiredScrollY;
desiredScrollX = -1;
desiredScrollY = -1;
scrollTo(nx, ny);
}
};
getViewTreeObserver().addOnGlobalLayoutListener(gol);
}
}
}
}
对我来说非常有用,因为我想在添加它之后立即滚动到ScrollView中的给定视图。
答案 3 :(得分:0)
尝试使用此功能:(kotlin)
myScrollView.post(Runnable { myScrollView.scrollTo(0, scrollValue) })