栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使ArrayAdapter遵循ViewHolder模式?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使ArrayAdapter遵循ViewHolder模式?

ViewHolder本质上是一个静态类实例,你在创建视图时将其与视图关联,从而在运行时将要查找的子视图缓存起来。如果视图已经存在,请检索holder实例并使用其字段而不是调用findViewById。

在你的情况下:

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {    View v = convertView;    ViewHolder holder; // to reference the child views for later actions    if (v == null) {        LayoutInflater vi =  (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);        v = vi.inflate(R.layout.mainrow, null);        // cache view fields into the holder        holder = new ViewHolder();        holder.nameText = (TextView) v.findViewById(R.id.nameText);        holder.priceText = (TextView) v.findViewById(R.id.priceText);        holder.changeText = (TextView) v.findViewById(R.id.changeText);        // associate the holder with the view for later lookup        v.setTag(holder);    }    else {        // view already exists, get the holder instance from the view        holder = (ViewHolder) v.getTag();    }    // no local variables with findViewById here    // use holder.nameText where you were     // using the local variable nameText before    return v;}// somewhere else in your class definitionstatic class ViewHolder {    TextView nameText;    TextView priceText;    TextView changeText;}

警告:我没有尝试编译它.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号