Last active
August 31, 2015 00:10
-
-
Save MrBenJ/a85fa16eb54ccc89f1cc to your computer and use it in GitHub Desktop.
Recycling Views using convertView == null and ViewHolder
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
// 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