Last active
June 14, 2018 08:40
-
-
Save jpardogo/3c087e036452e3c4bea8 to your computer and use it in GitHub Desktop.
Java classes to create a common base adapter strategy that acts as base for common adapters and multiple view types adapters in an android app
This file contains 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 BaseAllAdapters<T> extends BaseAdapter { | |
protected Context mContext; | |
protected LongSparseArray<DelegateAdapter> mDelegateAdapterSparseArray; | |
private int mViewLayoutId = 0; | |
private List<ListItem<T>> mItems; | |
/*Constructor if we dont use mutiple view types in the list*/ | |
public BaseAllAdapters(Context context, int viewLayoutId) { | |
mContext = context; | |
mViewLayoutId = viewLayoutId; | |
} | |
/*Constructor if we use the delegateAdapter to create multiple view types in the list*/ | |
public BaseAllAdapters(Context context, LongSparseArray<DelegateAdapter> delegateAdapterSparseArray) { | |
mContext = context; | |
Preconditions.checkNotNull(delegateAdapterSparseArray,"mDelegateAdapterSparseArray cannot be null"); | |
Preconditions.checkState(delegateAdapterSparseArray.size()!=0,"mDelegateAdapterSparseArray cannot be empty using a multiple view type adapter, If you dont want to specify muliple views, please use the \"BaseAllAdapters(Context context, int viewLayoutId)\" constructor."); | |
} | |
public void clear() { | |
mItems.clear(); | |
notifyDataSetChanged(); | |
} | |
public List<T> getItems() { | |
List<T> listItems = new ArrayList<T>(); | |
for (ListItem<T> listItem : mItems) { | |
listItems.add(listItem.getItem()); | |
} | |
return listItems; | |
} | |
public void setItems(List<T> items) { | |
List<ListItem<T>> listItems = Utils.createListItemList(items); | |
if (!Objects.equal(items, mItems)) { | |
mItems = listItems; | |
notifyDataSetChanged(); | |
} | |
} | |
@Override | |
public int getCount() { | |
return mItems != null ? mItems.size() : 0; | |
} | |
@Override | |
public ListItem<T> getItem(int position) { | |
return mItems.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
i | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
final ListItem<T> item = getItem(position); | |
ViewHolder holder; | |
if (convertView == null) { | |
if (isMultipleViewAdapter()) { | |
DelegateAdapter adapter = mDelegateAdapterSparseArray.get(getItemViewType(position)); | |
convertView = adapter.getView(mContext, position, convertView, parent, LayoutInflater.from(mContext), getItem(position)); | |
} else { | |
convertView = LayoutInflater.from(mContext).inflate(mViewLayoutId, parent, false); | |
} | |
holder = getHolderInstance(convertView); | |
if (holder == null) holder = new ViewHolder(convertView); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
bindView(position, convertView, item, holder); | |
return convertView; | |
} | |
private boolean isMultipleViewAdapter() { | |
return getViewTypeCount() > 1; | |
} | |
protected abstract void bindView(int position, View convertView, ListItem<T> item, ViewHolder parentHolder); | |
protected abstract ViewHolder getHolderInstance(View view); | |
public static class ViewHolder { | |
public ViewHolder(View view) { | |
ButterKnife.inject(this, view); | |
} | |
} | |
public static <T> List<ListItem<T>> createListItemList(final List<T> items) { | |
ArrayList<ListItem<T>> listItems = new ArrayList<ListItem<T>>(items.size()); | |
for (T item : items) { | |
listItems.add(new ListItem<T>(item)); | |
} | |
return listItems; | |
} | |
} |
This file contains 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 BaseDelegateAdapter<T> implements DelegateAdapter<T> { | |
private final int mViewLayoutId; | |
protected BaseDelegateAdapter(int viewLayoutId) { | |
mViewLayoutId = viewLayoutId; | |
} | |
@Override | |
public View getView(Context context, int position, View convertView, ViewGroup parent, LayoutInflater inflater, ListItem<T> item) { | |
BaseAllAdapters.ViewHolder holder; | |
if (convertView == null) { | |
convertView = LayoutInflater.from(context).inflate(mViewLayoutId, parent, false); | |
holder = getHolderInstance(convertView); | |
if(holder== null) holder = new BaseAllAdapters.ViewHolder(convertView); | |
convertView.setTag(holder); | |
}else{ | |
holder = (BaseAllAdapters.ViewHolder) convertView.getTag(); | |
} | |
bindView(position, convertView, item, holder); | |
return convertView; | |
} | |
protected abstract void bindView(int position, View convertView, ListItem<T> item, BaseAllAdapters.ViewHolder parentHolder); | |
protected abstract BaseAllAdapters.ViewHolder getHolderInstance(View convertView); | |
} |
This file contains 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 interface DelegateAdapter<T> { | |
public View getView(Context context, int position, View convertView, ViewGroup parent, LayoutInflater inflater, ListItem<T> item); | |
} |
This file contains 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 class ListItem<T> { | |
public static final int TYPE_ITEM = 0; | |
public static final int TYPE_SECTION = 1; | |
private final int type; | |
private final T item; | |
private final String sectionTitle; | |
public ListItem(T item) { | |
type = TYPE_ITEM; | |
this.item = item; | |
sectionTitle = null; | |
} | |
public ListItem(String sectionTitle) { | |
type = TYPE_SECTION; | |
item = null; | |
this.sectionTitle = sectionTitle; | |
} | |
public int getType() { | |
return type; | |
} | |
public T getItem() { | |
return item; | |
} | |
public String getSectionTitle() { | |
return sectionTitle; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
ListItem listItem = (ListItem) o; | |
if (type != listItem.type) { | |
return false; | |
} | |
if (item != null ? !item.equals(listItem.item) : listItem.item != null) { | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = type; | |
result = 31 * result + (item != null ? item.hashCode() : 0); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment