Android的滚动型,在可见的滚动 [英] Android ScrollView, scroll before visible
问题描述
我怎么可以设置滚动型滚动到x像素,之前它甚至显示?
how can I set scroll of scrollview to x pixels, before it's even shown?
在滚动型我有一些意见,而我知道,这将填补屏幕。我可以滚动,让说的第二种观点,使第一个视图时,活动开始是不可见的?
In ScrollView I have some Views and i know that it will fill screen. Can I scroll to let say second View, so that first View is not visible when activity is started?
现在我有某事像这样,但我认为这是不完美的,有时,我看到的第一视图,然后它的滚动到第二个
Now I have sth like this, but I think it's not perfect, sometimes, I see first View, and then it's scrolled to the second one
@Override
public void onGlobalLayout() {
if (mHorizontalScrollView.getChildCount() > 0 && mHorizontalScrollView.getChildAt(0).getWidth() > mScreenWidth) {
hsv.scrollTo(100, 0);
}
}
编辑!!
第二次尝试是使用的http://developer.android.com/reference/android/view/ViewTreeObserver.On$p$pDrawListener.html代替<一个href="http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html" rel="nofollow">http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html
在在preDrawListener
,我们可以读到在这一点上,树中的所有意见进行了测量,并给出一个框架。客户端可以用这个来调整自己的滚动界限,甚至要求一个新的布局图发生之前。
所以基本上在这一点上,我们应该调整滚动。所以,我创建了:
Second attempt was to use http://developer.android.com/reference/android/view/ViewTreeObserver.OnPreDrawListener.html instead of http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html
In OnPreDrawListener
we can read that At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs.
so basically at this point we should adjust scrolling. So I created:
@Override
public boolean onPreDraw() {
if (hsv.getChildCount() > 0 && hsv.getChildAt(0).getWidth() > mScreenWidth) {
hsv.scrollTo(100, 0);
return false;
}
return true;
}
但现在它从来没有为我工作。
but now it's never working for me.
推荐答案
我结合 onGlobalLayout
低于code。这不是那么伟大的使用技巧,但它仍然相当有效率。当我onGlobalLayout事件中,我检查,如果布局完成,那么,如果它的确定我使用下面的方法。
I combined onGlobalLayout
with code below. It's not so great to use that hack but it still quite efficient. When I get onGlobalLayout event I check if layouts are done and then if it's ok I use method below.
// hsv - horizontal scroll view
private void forceShowColumn(final int column) {
int scrollTo = childViewWidth * column;
if (scrollTo == hsv.getScrollX()) {
return;
}
hsv.scrollTo(scrollTo, 0);
hsv.postDelayed(new Runnable() {
@Override
public void run() {
forceShowColumn(column);
}
}, 10);
}
这篇关于Android的滚动型,在可见的滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!