Last active
August 26, 2015 11:01
-
-
Save dinabandhuM/d2b7f1cb2d869e50905e to your computer and use it in GitHub Desktop.
ViewHolder for list and grid view android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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