优化 Adapter 中 getView 方法的使用方式
Optimized way to use getView method in Adapter
明白了,每次都调用view.findViewById(R.id.title);是非常昂贵的操作。我如何通过存储这些 int 值来优化它。 List Adapter 的优化使用方式是什么?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final DataObject dataObject = getItem(position);
if (view == null) {
// No view created yet
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(layoutResourceId, parent, false);
}
if (view != null) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView price = (TextView) view.findViewById(R.id.price);
ImageView img = (ImageView) view.findViewById(R.id.profile_pic);
RatingBar overallRating = (RatingBar) view.findViewById(R.id.overall_rating);
你应该像这样使用 viewholder:
ViewHolder holder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.chocolate_list_item,
parent, false);
holder.txtvwChocolateName = (TextView) convertView
.findViewById(R.id.xtxtvwChocolateName);
holder.imgvwChocolateIcon=(ImageView)convertView.findViewById(R.id.ximgvwChocolateIcon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtvwChocolateName.setText(alChocolate.get(position)
.getStrChocolateName());
holder.imgvwChocolateIcon.setImageResource(images[position]);
return convertView;
}
class ViewHolder {
TextView txtvwChocolateName;
ImageView imgvwChocolateIcon;
}
明白了,每次都调用view.findViewById(R.id.title);是非常昂贵的操作。我如何通过存储这些 int 值来优化它。 List Adapter 的优化使用方式是什么?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final DataObject dataObject = getItem(position);
if (view == null) {
// No view created yet
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(layoutResourceId, parent, false);
}
if (view != null) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView price = (TextView) view.findViewById(R.id.price);
ImageView img = (ImageView) view.findViewById(R.id.profile_pic);
RatingBar overallRating = (RatingBar) view.findViewById(R.id.overall_rating);
你应该像这样使用 viewholder:
ViewHolder holder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.chocolate_list_item,
parent, false);
holder.txtvwChocolateName = (TextView) convertView
.findViewById(R.id.xtxtvwChocolateName);
holder.imgvwChocolateIcon=(ImageView)convertView.findViewById(R.id.ximgvwChocolateIcon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtvwChocolateName.setText(alChocolate.get(position)
.getStrChocolateName());
holder.imgvwChocolateIcon.setImageResource(images[position]);
return convertView;
}
class ViewHolder {
TextView txtvwChocolateName;
ImageView imgvwChocolateIcon;
}