ScrollView嵌套ListView自动聚焦到ListView的问题

<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);

官方解释:

Open Declarationvoidandroid.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。有大牛的可以给我解释解释,谢谢啦!





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值