Back to project page Vispin.
The source code is released under:
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. ...
If you think the Android project Vispin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jpardogo.vispin.adapters; //www.ja v a 2 s. c o m import android.content.Context; import android.support.v4.util.LongSparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.jpardogo.vispin.adapters.delegates.DelegateAdapter; import com.jpardogo.vispin.models.ListItem; import com.jpardogo.vispin.utils.Utils; import java.util.ArrayList; import java.util.List; import butterknife.ButterKnife; 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) { mContext = context; mDelegateAdapterSparseArray = new LongSparseArray<DelegateAdapter>(); initDelegates(); if(mDelegateAdapterSparseArray.size()==0){ throw new IllegalArgumentException("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."); } } protected abstract void initDelegates(); 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; } @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); } } }