Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Forked from JakeWharton/BindingAdapter.java
Last active December 20, 2015 07:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save patrickhammond/6094827 to your computer and use it in GitHub Desktop.
Save patrickhammond/6094827 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
* An implementation of {@link BaseAdapter} which uses the new/bind pattern and
* view holder pattern for its views.
*
* Inspired by/baesd on Jake Wharton's BindingAdapter:
* https://gist.github.com/JakeWharton/5423616
*
* @param <T> The type of item being displayed.
* @param <ViewHolder> The view holder type being used.
*/
public abstract class BindingAdapter<T, ViewHolder> extends BaseAdapter {
private final Context context;
private final LayoutInflater inflater;
public BindingAdapter(Context context) {
this.context = context;
this.inflater = LayoutInflater.from(context);
}
public Context getContext() {
return context;
}
@Override public abstract T getItem(int position);
@Override public final View getView(int position, View view, ViewGroup container) {
if (view == null) {
view = newView(inflater, position, container);
if (view == null) {
throw new IllegalStateException("newView result must not be null.");
}
associateViewHolder(view);
}
bindView(getItem(position), position, view, getViewHolder(view));
return view;
}
/** Create a new instance of a view for the specified position. */
public abstract View newView(LayoutInflater inflater, int position, ViewGroup container);
/**
* If your ViewHolder implementation looks something like this:
* <pre>
* {@code
* static class ViewHolder {
* final TextView textView;
*
* ViewHolder(View view) {
* textView = (TextView) view.findViewById(R.id.textView);
* }
* }
* </pre>
*
* This method only needs this as its implementation:
* <pre>
* {@code
* return new ViewHolder(view);
* }
* </pre>
*
* If implementations do not need/want a view holder, just return <code>null</code>.
*/
public abstract ViewHolder buildViewHolder(View view);
/**
* Bind the data for the specified {@code position} to the view using a
* {@code viewHolder} created from {@link #buildViewHolder(View)}.
*/
public abstract void bindView(T item, int position, View view, ViewHolder viewHolder);
@Override public final View getDropDownView(int position, View view, ViewGroup container) {
if (view == null) {
view = newDropDownView(inflater, position, container);
if (view == null) {
throw new IllegalStateException("newDropDownView result must not be null.");
}
associateViewHolder(view);
}
bindDropDownView(getItem(position), position, view, getViewHolder(view));
return view;
}
private void associateViewHolder(View view) {
ViewHolder viewHolder = buildViewHolder(view);
view.setTag(viewHolder);
}
@SuppressWarnings("unchecked")
private ViewHolder getViewHolder(View view) {
return (ViewHolder) view.getTag();
}
/** Create a new instance of a drop-down view for the specified position. */
public View newDropDownView(LayoutInflater inflater, int position, ViewGroup container) {
return newView(inflater, position, container);
}
/** Bind the data for the specified {@code position} to the drop-down view. */
public void bindDropDownView(T item, int position, View view, ViewHolder viewHolder) {
bindView(item, position, view, viewHolder);
}
}
import android.content.Context;
import java.util.Collections;
import java.util.List;
/**
* Specialized binding adapter that handles some of the boilerplate associated with lists.
* @param <T> The type of item being displayed.
* @param <ViewHolder> The view holder type being used.
*/
public abstract class BindingListAdapter<T, ViewHolder> extends BindingAdapter<T, ViewHolder> {
private final List<T> items;
public BindingListAdapter(Context context, List<T> items) {
super(context);
this.items = Collections.unmodifiableList(items);
}
@Override
public T getItem(int position) {
return items.get(position);
}
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int i) {
return i;
}
}
@patrickhammond
Copy link
Author

Discuss: make returning a view holder object mandatory. The only places I can find where I haven't really benefited from using one before is where I inflated a single view layout that didn't require findViewById calls during binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment