当我创建一个类 extends BaseAdapter
时,我收到一条警告消息
来自 View 适配器的无条件布局膨胀:应该使用 View Holder 模式(使用循环 View 作为第二个参数传递到此方法中)以实现更平滑的滚动
我需要按照这个建议更改我的代码吗?我所有的代码都运行顺利,只是是否有必要
更改代码以使用最新样式进行updata代码?或者只需要添加 @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;
}
如果你想按照上面警告的建议更改我的代码,以后喜欢我的代码吗?对不起,如果我的问题对初学者来说太基础了
最佳答案
如果您的 ListView 中有更多数据,那么您应该使用回收,因为它还可以改善滚动和性能。如果您在适配器中使用 View 持有者,则代码如下所示。
你的 ViewHolder 看起来像
public class ViewHolder {
public TextView merchant_type;
public TextView merchant_name;
public TextView merchant_location;
}
和您的 getView 方法
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;
关于java - 我应该为我的适配器使用 View Holder 模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29230403/