<span style="font-size: 18px;">ScrollView嵌套ListView的高度问题顺带再写一遍吧。自定义</span><span style="font-family: Arial, Helvetica, sans-serif;">VerticalListview </span>继承ListView,重写OnMeasure方法。以此类推,ListView嵌套ListView也是同样的方法。<strong>至于为什么,我还没研究透,看到的大牛们可以帮帮小妹,在下感谢。</strong>
public class VerticalListview extends ListView {
public VerticalListview(Context context) {
super(context);
}
public VerticalListview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalListview(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
重点来了。ScrollView嵌套ListView(中奖名单),当ListView被填充时,ScrollView就滚动到ListView上部,相信很多人都遇到过这个问题。
解决方法:在ListView填充完毕时调用:scrollView.smoothScrollTo(0, 0);
官方解释:
voidandroid.widget.ScrollView.smoothScrollTo(int x, int y)
Like scrollTo
, but scroll smoothly instead of immediately.
-
Parameters:
- x the position where to scroll on the X axis
- y the position where to scroll on the Y axis
-
-
smoothScrollTo是顺滑的移动,按理说scrollTo也可以的,但是,scrollTo实际上并没有移动上去,只能用smoothScrollTo。有大牛的可以给我解释解释,谢谢啦!