![](/img/trans.png)
[英]ScrollTo() method doesn't work when WebView inside ScrollView
[英]ScrollView scrollTo doesn't work
與ScrollView .scrollTo 不工作相同的問題? 在旋轉時保存 ScrollView 位置
我在 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”這樣的事件? 有回調嗎?
您不需要擴展 ScrollView 並根據David Daudelin為這個問題提供的答案創建自己的。
獲取ViewTreeObserver
中的ScrollView
和添加OnGlobalLayoutListener
到ViewTreeObserver
。 然后調用ScrollView.scrollTo(x,y)
從該方法onGlobalLayout()
的方法OnGlobalLayoutListener
。 代碼:
ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
mainScroll.scrollTo(0, 0);
}
});
這對我scrollView.scrollTo()
,將scrollView.scrollTo()
替換為
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.scrollTo(0, ScrollViewYPosition);//0 is x position
}
});
瓦西里·卡布諾夫 (Vasily Kabunov) 的回答幫助了我
當您對 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 中的給定視圖。
試試這個它有效:(kotlin)
myScrollView.post(Runnable { myScrollView.scrollTo(0, scrollValue) })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.