*getView 千年不变

本文详细介绍了在Android应用开发中如何使用Adapter模式结合ViewHolder模式来优化列表视图的性能。从inflate方法的作用到ViewHolder的创建过程,再到如何绑定数据及实现item的复用,文章深入浅出地讲解了这一关键流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /**此方法用于构建item,每显示一个item都要执行一次此方法*/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {//重要
        //1.item view (饺子皮,饺子皮的样子由mResouce决定)
        View v=null;/**1*/
        ViewHolder vHolder=null;/**1*/
        if(convertView==null){//页面启动时初始值为空/**3*/
        //inflate方法返回的类型由mResouce资源文件的根元素决定
        //1)根据资源id找到对应的xml布局资源
        //2)底层会借助pull解析解析xml资源
        //3)底层会通过反射构建xml资源中的元素对象
        //4)inflate方法的返回值为mResource资源文件的根元素对象
        v=mInflater.inflate(mResouce,parent,false);/**3布局构造器 */
        vHolder=new ViewHolder();/**4创建VH,给子view赋值*/
        vHolder.sectionTv=(TextView) v.findViewById(R.id.sectionId);
        vHolder.titleTv=(TextView) v.findViewById(android.R.id.text1);
        vHolder.createTv=(TextView) v.findViewById(android.R.id.text2);
        v.setTag(vHolder);//建立关联:相当于一个教室一个座位表
        }else{
        v=convertView;//可重用的item view对象
        vHolder=(ViewHolder) v.getTag();
        }
       
        //2.item data (饺子馅)/**5*/
        Music m=mObjects.get(position);
       
        //3.bind view (包饺子,对号入座,将饺子馅放到饺子皮上)/**6*/
        vHolder.sectionTv.setText(m.getTitle().substring(0, 1).toUpperCase());
        vHolder.titleTv.setText(m.getTitle());
        vHolder.createTv.setText(m.getCreated());
       
        /**7 后面就是做监听分块等具体业务的事了*/
        //4.设置item view背景(根据选中位置的不同进行设置)
        int checked=((ListView)parent).
                getCheckedItemPosition();
        if(position==checked){
            v.setBackgroundColor(Color.YELLOW);
        }else{
            v.setBackgroundColor(Color.WHITE);
        }
       
        //5.显示或隐藏item对应的section
        int section=getSectionForPosition(position);
        int minPos=getPositionForSection(section);
        if(position==minPos){
        vHolder.sectionTv.setVisibility(View.VISIBLE);
        }else{
        vHolder.sectionTv.setVisibility(View.GONE);
        }
        return v;//包好的饺子/**1*/
    }
    /**通过此方法获得每个位置的section的ascii值*/
    public int getSectionForPosition(int position){
        return mObjects.get(position).
        getTitle().toUpperCase().charAt(0);//char返回成int,就是ascii码
    }
    /***通过此方法获得section在列表中的最小位置*/
    public int getPositionForSection(int section){//section传过来的大写字母 findSec查到的大写字母
        //1.遍历所有位置(0~getCount()-1)
        for(int i=0;i<getCount();i++){
         int findSec=getSectionForPosition(i);
         if(findSec==section){
        //2.找到最小位置
             return i;
         }
        }
        return -1;
    }
    class ViewHolder{/**2写ViewHolder,声明子view*/
        TextView sectionTv;
        TextView titleTv;
        TextView createTv;
    }
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值