Skip to content

Instantly share code, notes, and snippets.

@dinabandhuM
Last active August 26, 2015 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinabandhuM/d2b7f1cb2d869e50905e to your computer and use it in GitHub Desktop.
Save dinabandhuM/d2b7f1cb2d869e50905e to your computer and use it in GitHub Desktop.
ViewHolder for list and grid view android
protected static class ViewHolderProductList {
// I added a generic return type to reduce the casting noise in client
// code
@SuppressWarnings("unchecked")
public static <T extends View> T get(View view, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
}
/* use in getView() */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(
R.layout.row_home_supersonic_item_listing, parent, false);
}
ImageView advertisingAppImg = ViewHolderProductList.get(convertView,
R.id.row_home_ivAppThumbnail);
TextView advertisingAppName = ViewHolderProductList.get(convertView,
R.id.row_home_tvAddAppTitle);
TextView tvCredits = ViewHolderProductList.get(convertView,
R.id.row_home_tvCredits);
return convertView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment