Skip to content

Instantly share code, notes, and snippets.

@johnkil
Forked from JakeWharton/ViewHoldingAdapter.java
Created April 26, 2012 18:31
Show Gist options
  • Save johnkil/2501719 to your computer and use it in GitHub Desktop.
Save johnkil/2501719 to your computer and use it in GitHub Desktop.
Template for a list adapter which uses a view holder to cache lookups.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh = ViewHolder.get(convertView, parent);
Item item = getItem(position);
vh.title.setText(item.title);
vh.subtitle.setText(item.subtitle);
return vh.root;
}
public class ViewHolder {
public static ViewHolder get(View convertView, ViewGroup parent) {
if (convertView == null) {
return new ViewHolder(parent);
}
return (ViewHolder)convertView.getTag();
}
public final View root;
public final TextView title;
public final TextView subtitle;
private ViewHolder(ViewGroup parent) {
root = LayoutInflater.from(parent.getContext()).inflate(R.layout.whatever, parent, false);
root.setTag(this);
title = (TextView) root.findViewById(R.id.title);
subtitle = (TextView) root.findViewById(R.id.subtitle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment