Skip to content

Instantly share code, notes, and snippets.

@Flydiverny
Created September 26, 2015 15:51
Show Gist options
  • Select an option

  • Save Flydiverny/1d82e5e106e4f4aa5ab8 to your computer and use it in GitHub Desktop.

Select an option

Save Flydiverny/1d82e5e106e4f4aa5ab8 to your computer and use it in GitHub Desktop.
Just a simple ArrayAdapter enforcing a viewholder pattern.
public abstract class VHAdapter<T, VH> extends ArrayAdapter<T> {
@LayoutRes
private final int mLayoutRes;
private final LayoutInflater mInflater;
public VHAdapter(Context context, int resource) {
this(context, resource, new ArrayList<T>());
}
public VHAdapter(Context context, int resource, List<T> items) {
super(context, resource, items);
mLayoutRes = resource;
mInflater = LayoutInflater.from(context);
}
@Override
@SuppressWarnings("unchecked")
public final View getView(int position, View convertView, ViewGroup parent) {
// No view to update
if (convertView == null) {
convertView = mInflater.inflate(mLayoutRes, parent, false);
convertView.setTag(createViewHolder(convertView));
}
updateView(getItem(position), (VH) convertView.getTag(), position);
return convertView;
}
protected abstract void updateView(T item, VH viewHolder, int position);
protected abstract VH createViewHolder(View view);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment