HorizontalListView 的selection方法


通过getAdapter的getView方法来获取子View的宽度,进行scrollTo();

@Override

    public void setSelection(int position) {
         float addWidth = 0;
         Log.e("++++getChildCount()++++"+getAdapter().getCount());
         //
         for (int i = 0; i < position; i++) {
             View listItem = getAdapter().getView(i, null, this);
             listItem.measure(0, 0);  
             addWidth +=listItem.getMeasuredWidth();
         }
         scrollTo((int) addWidth);  

    }

记得调用的时候只能在@Override
    public void onWindowFocusChanged(boolean hasFocus) {
     // TODO Auto-generated method stub
     super.onWindowFocusChanged(hasFocus);
         if(hasFocus){
             setSelection();
         }
    }中使用,即view加载完毕,listView的所有子View都填充完了,才能调用getAdapter()方法的getView来获取所有View的宽度,

、、、换句话说就是在setAdapter,填充数据后调用

另外提醒大家

切记区分getChildViewAt()、getChildCount()   和   getAdapter().getView()、getAdapter().getCount()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值