Skip to content

Instantly share code, notes, and snippets.

@twaddington
Created December 11, 2012 20:38
Show Gist options
  • Save twaddington/4261943 to your computer and use it in GitHub Desktop.
Save twaddington/4261943 to your computer and use it in GitHub Desktop.
/**
* Instances of this class can be used to safely retain references to
* <code>View</code> objects.
*
* <p>
* Prior to Android 4.0 storing a <code>View</code> as a tag with the
* {@link View#setTag(int, Object)} method could <a
* href="http://code.google.com/p/android/issues/detail?id=18273">result in
* memory leaks</a>. Instead, the {@link View#setTag(Object)} method should be
* used with a <code>ViewHolder</code> instance as the object.
* </p>
*
* @author Tristan Waddington
*/
public class ViewHolder {
private SparseArray<View> mViewHolder;
public ViewHolder() {
mViewHolder = new SparseArray<View>();
}
public void setView(int id, View view) {
mViewHolder.put(id, view);
}
public View getView(int id) {
return mViewHolder.get(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment