我应该使用View Holder图案作为适配器吗? [英] Should I use View Holder pattern for my adapter?

查看:148
本文介绍了我应该使用View Holder图案作为适配器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个类 extends BaseAdapter 时,我收到一条警告消息

When I create a class extends BaseAdapter, I get a warning message

来自视图适配器的无条件布局膨胀:应使用View Holder模式(使用传递给此方法的循环视图作为第二个参数)以实现更顺畅的滚动

我是否需要像这个建议一样更改我的代码?我的所有代码都运行顺畅,只是必需更改代码以使用最新样式执行更新代码?或者只需要添加 @SuppressLint({ViewHolder,InflateParams})

Do I need to change my code like this suggestion? All my code is running smoothly, it's just whether it is necessary to change the code to do updata code with the latest styles? Or just need to add @SuppressLint({ "ViewHolder", "InflateParams" }) ?

我的适配器

public View getView(int position, View convertView, ViewGroup parent) {
    View v = inflater.inflate(R.layout.list_item, null);

    TextView merchant_type = (TextView) v.findViewById(R.id.merchant_type);
    TextView merchant_name = (TextView) v.findViewById(R.id.merchant_name);
    TextView merchant_location = (TextView) v.findViewById(R.id.merchant_location);

    VoucherBean obj = (VoucherBean) getItem(position);

    merchant_type.setText(obj.getMerchantType());
    merchant_name.setText(obj.getMerchantName());
    merchant_location.setText(obj.getMerchantLocation());

    return v;
}

如果你想按照上面推荐的警告更改我的代码,就像我的代码一样后来?很抱歉,如果我的问题对初学者来说太基本了

If you want to change my code as recommended above warning, like what my code later? Sorry if my question is too basic for a beginner

推荐答案

如果您的列表视图中有更多数据,那么您应该使用回收,因为它也改善了滚动和性能。如果您在适配器中使用视图持有者,则代码如下。

if you have more data in your list view then you should use recycling, because it improves scrolling and performance also. here is code look like if you use view holder in your adapter.

您的ViewHolder看起来像

your ViewHolder looks like

public  class ViewHolder {

        public TextView merchant_type;
        public TextView merchant_name;
        public TextView merchant_location;

    }

和你的getView方法

and your getView method

    View vi = convertView;
        ViewHolder holder;

        if (convertView == null) {

            vi = inflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.merchant_type  = (TextView) vi.findViewById(R.id.merchant_type);
            holder.merchant_name  = (TextView) vi.findViewById(R.id.merchant_name);
            holder.merchant_location  = (TextView) vi.findViewById(R.id.merchant_location);

            vi.setTag(holder);

        } else
            holder = (ViewHolder) vi.getTag();

        // now set your text view here like 

          holder.merchant_name.setText("Bla Bla Bla");


        // return your view
        return vi;

这篇关于我应该使用View Holder图案作为适配器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆