Skip to content

Instantly share code, notes, and snippets.

@kevinmrohr
Forked from patrickhammond/BindingAdapter.java
Last active December 25, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmrohr/7043835 to your computer and use it in GitHub Desktop.
Save kevinmrohr/7043835 to your computer and use it in GitHub Desktop.
BindingAdapter now receives a List (could be a Collection if we want to make it more generic), and provides default implementations of getItem(), getCount() and getItemId()
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
* An implementation of {@link android.widget.BaseAdapter} which uses the new/bind pattern and
* view holder pattern for its views.
* <p/>
* Inspired by/baesd on Jake Wharton's BindingAdapter: https://gist.github.com/JakeWharton/5423616
* <p/>
* Pulled from Patrick Hammond's https://gist.github.com/patrickhammond/6094827
*/
public abstract class BindingAdapter<T, S> extends BaseAdapter {
protected List<T> dataList;
private final Context context;
private final LayoutInflater inflater;
public BindingAdapter(Context context, List<T> dataList) {
this.context = context;
this.dataList = dataList;
this.inflater = LayoutInflater.from(context);
}
@Override
public T getItem(int position) {
return dataList.get(position);
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public long getItemId(int position) {
return position;
}
public Context getContext() {
return context;
}
@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, position);
}
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>
* <p/>
* This method only needs this as its implementation:
*
* <pre>
* {@code
* return new ViewHolder(view);
* }
* </pre>
* <p/>
* If implementations do not need/want a view holder, just return <code>null</code>.
*/
public abstract S buildViewHolder(View view, int position);
/**
* Bind the data for the specified {@code position} to the view using a {@code viewHolder} created from
* {@link #buildViewHolder(android.view.View, int)}.
*/
public abstract void bindView(T item, int position, View view, S vh);
@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, position);
}
bindDropDownView(getItem(position), position, view, getViewHolder(view));
return view;
}
private void associateViewHolder(View view, int position) {
S viewHolder = buildViewHolder(view, position);
view.setTag(viewHolder);
}
@SuppressWarnings("unchecked")
private S getViewHolder(View view) {
return (S) 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, S viewHolder) {
bindView(item, position, view, viewHolder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment