我有一个 gridView,我在其中使用自定义适配器填充数据。为此,我重写了 getView() 方法。但是,由于我的 gridView 是自定义的,我不得不使用静态持有者类(如在线提供的许多示例中所见)。如果我不想使用持有人,我还能以什么其他方式实现它?为什么要使用 getTag() 和 setTag() ?我搜索了它们,但找不到足够的解释。
持有人类 -
static class RecordHolder{
ImageView imgView;
TextView txtView;
}
getView 方法 -
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = new RecordHolder();
if (convertView == null) {
LayoutInflater inflater = (getActivity()).getLayoutInflater();
row = inflater.inflate(R.layout.grid_line, parent, false);
holder.imgView = (ImageView) row.findViewById(R.id.imageView);
holder.txtView = (TextView) row.findViewById(R.id.textView);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
ResolveInfo resolveInfo = mAppList.get(position);
holder.imgView.setImageDrawable(resolveInfo.loadIcon(getActivity().getPackageManager()));
holder.txtView.setText(resolveInfo.loadLabel(getActivity().getPackageManager()));
return row;
}