我试图在我的Android应用程序中实现ViewHolder,但是我一直收到ViewHolder无法解析为类型的错误,没有任何导入建议。有人知道如何解决这个问题吗?
static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}
**Create a Holder class**
protected static class ViewHolderItems
{
private ImageView mStoreImage;
private TextView mStoreName;
}
And use In getView method of adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems viewHolder;
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.coupen_row, null);
viewHolder = new ViewHolderItems();
viewHolder.mStoreImage = (ImageView) convertView.findViewById(R.id.storeImage);
viewHolder.mStoreName = (TextView) convertView.findViewById(R.id.storeName);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolderItems) convertView.getTag();
}
return convertView;
}
也许你正在寻找 RecyclerView.ViewHolder,它是 Android 支持库的一部分。
就像这个链接中的代码 Paul Burke 的 gist 一样。
public static class ItemViewHolder extends RecyclerView.ViewHolder implements
ItemTouchHelperViewHolder {
public final TextView textView;
public final ImageView handleView;
public ItemViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
handleView = (ImageView) itemView.findViewById(R.id.handle);
}
@Override
public void onItemSelected() {
itemView.setBackgroundColor(Color.LTGRAY);
}
@Override
public void onItemClear() {
itemView.setBackgroundColor(0);
}
}
如果你正在使用Android RecyclerView,这将对你有意义。
在这种情况下,他们需要一个对象来保存视图,以便根据需要填充内容。