use*_*151 5 android scrollview
与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);
Run Code Online (Sandbox Code Playgroud)
我得出结论,有一些事件像'DOM-ready'?有没有回调?
fah*_*hmy 17
你并不需要延长滚动型,并创建自己按照所提供的答案大卫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);
}
});
Run Code Online (Sandbox Code Playgroud)
小智 7
这对我有用,替换scrollView.scrollTo()为
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.scrollTo(0, ScrollViewYPosition);//0 is x position
}
});
Run Code Online (Sandbox Code Playgroud)
瓦西里·卡布诺夫 (Vasily Kabunov) 的回答帮助了我
ScrollView .scrollTo 不起作用?在旋转时保存 ScrollView 位置
当您对 ScrollView 进行一些更改时,它需要一段时间才能将布局更改复制到显示列表并通知 ScrollView 它确实可以滚动到某个位置。
我已经设法通过扩展ScrollView我自己的 ScrollView 并添加一个方法来使其工作,该方法OnGlobalLayoutListener根据需要添加(如 MH 建议的)并稍后滚动到那里。与将其应用于您需要的每个案例相比,它的自动化程度更高(但随后您需要使用 new 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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说非常有用,因为我想在添加后立即滚动到 ScrollView 内的给定视图。
| 归档时间: |
|
| 查看次数: |
11042 次 |
| 最近记录: |