-
-
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.
This file contains 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
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