Skip to content

Instantly share code, notes, and snippets.

@MrBenJ
Last active August 31, 2015 00:10
Show Gist options
  • Save MrBenJ/a85fa16eb54ccc89f1cc to your computer and use it in GitHub Desktop.
Save MrBenJ/a85fa16eb54ccc89f1cc to your computer and use it in GitHub Desktop.
Recycling Views using convertView == null and ViewHolder
// This is the @Override getView() portion of the Adapter class - recycles views in order to create a faster
// and much more efficient UI. Boosts framerates from 15 FPS to 50 FPS due to savvy memory allocation.
private LayoutInflater mInflater;
private List<SomeObject> mList;
// Constructor for Adapter object
public nameOfAdapter(Context context, List<SomeObject> myListOfObjects) {
mInflater = LayoutInflater.from(context);
mList = myListOfObjects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.yourCustomList, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textview);
holder.icon = (ImageView) convertView.findViewById(R.id.imageview);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment