Created
September 26, 2015 15:51
-
-
Save Flydiverny/1d82e5e106e4f4aa5ab8 to your computer and use it in GitHub Desktop.
Just a simple ArrayAdapter enforcing a viewholder pattern.
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
| public abstract class VHAdapter<T, VH> extends ArrayAdapter<T> { | |
| @LayoutRes | |
| private final int mLayoutRes; | |
| private final LayoutInflater mInflater; | |
| public VHAdapter(Context context, int resource) { | |
| this(context, resource, new ArrayList<T>()); | |
| } | |
| public VHAdapter(Context context, int resource, List<T> items) { | |
| super(context, resource, items); | |
| mLayoutRes = resource; | |
| mInflater = LayoutInflater.from(context); | |
| } | |
| @Override | |
| @SuppressWarnings("unchecked") | |
| public final View getView(int position, View convertView, ViewGroup parent) { | |
| // No view to update | |
| if (convertView == null) { | |
| convertView = mInflater.inflate(mLayoutRes, parent, false); | |
| convertView.setTag(createViewHolder(convertView)); | |
| } | |
| updateView(getItem(position), (VH) convertView.getTag(), position); | |
| return convertView; | |
| } | |
| protected abstract void updateView(T item, VH viewHolder, int position); | |
| protected abstract VH createViewHolder(View view); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment