package zhan.auto_adapter; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by ruzhan on 2017/5/1. */ public class AutoRecyclerAdapter extends RecyclerView.Adapter { protected List packageList = new ArrayList<>(); protected List dataList = new ArrayList<>(); protected SparseArray holderPackageMap = new SparseArray<>(); private SparseArray holderConstructorMap = new SparseArray<>(); protected List holderList = new ArrayList<>(); @Override public int getItemViewType(int position) { return packageList.get(position).getType(); } @SuppressWarnings({ "unchecked", "TryWithIdenticalCatches" }) @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { AutoHolderPackage holderPackage = holderPackageMap.get(viewType); if (holderPackage == null) { throw new RuntimeException( "not find viewType is: ( " + viewType + " ) holder, viewType error"); } int holderLayoutRes = holderPackage.getHolderLayoutRes(); View itemView = LayoutInflater.from(parent.getContext()).inflate(holderLayoutRes, parent, false); Class holderClass = holderPackage.getHolderClass(); Map dataMap = holderPackage.getDataMap(); try { Constructor constructor = holderConstructorMap.get(viewType); if (constructor == null) { constructor = holderClass.getConstructor(View.class, Map.class); holderConstructorMap.put(viewType, constructor); } AutoHolder autoHolder = (AutoHolder) constructor.newInstance(itemView, dataMap); holderList.add(autoHolder); return autoHolder; } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } throw new RuntimeException("( " + holderClass + " ) constructor error"); } @SuppressWarnings("unchecked") @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof AutoHolder) { AutoHolder autoHolder = (AutoHolder) holder; Object bean = packageList.get(position).getAutoPackage(); autoHolder.bind(position, bean); } } @Override public int getItemCount() { return packageList.size(); } public int getSpanSize(int position) { return packageList.get(position).getSpanSize(); } ///////////////////////////////////////////////////////////////////////// // set holder ///////////////////////////////////////////////////////////////////////// public AutoRecyclerAdapter setHolder(Integer key, Class holderClass, int layoutRes) { holderPackageMap.put(key, new AutoHolderPackage<>(holderClass, layoutRes)); return this; } public AutoRecyclerAdapter setHolderToData(Integer key, Class holderClass, int layoutRes, Map dataMap) { holderPackageMap.put(key, new AutoHolderPackage<>(holderClass, layoutRes, dataMap)); return this; } public AutoRecyclerAdapter setHolderToListener(Integer key, Class holderClass, int layoutRes, Map dataMap, OnAutoHolderListener listener) { dataMap.put(AutoHolder.LISTENER, listener); holderPackageMap.put(key, new AutoHolderPackage<>(holderClass, layoutRes, dataMap)); return this; } public AutoRecyclerAdapter setHolder(Class holderClass, int layoutRes) { return setHolder(holderClass.hashCode(), holderClass, layoutRes); } public AutoRecyclerAdapter setHolderToListener(Class holderClass, int layoutRes, OnAutoHolderListener listener) { Map dataMap = new HashMap<>(); dataMap.put(AutoHolder.LISTENER, listener); return setHolderToData(holderClass.hashCode(), holderClass, layoutRes, dataMap); } public AutoRecyclerAdapter setHolderToData(Class holderClass, int layoutRes, String key, Object value) { Map dataMap = new HashMap<>(); dataMap.put(key, value); return setHolderToData(holderClass.hashCode(), holderClass, layoutRes, dataMap); } public AutoRecyclerAdapter setHolderToListener(Class holderClass, int layoutRes, String key, Object value, OnAutoHolderListener listener) { Map dataMap = new HashMap<>(); dataMap.put(key, value); dataMap.put(AutoHolder.LISTENER, listener); return setHolderToData(holderClass.hashCode(), holderClass, layoutRes, dataMap); } public AutoRecyclerAdapter setHolderToData(Class holderClass, int layoutRes, Map dataMap) { return setHolderToData(holderClass.hashCode(), holderClass, layoutRes, dataMap); } public AutoRecyclerAdapter setHolderToListener(Class holderClass, int layoutRes, Map dataMap, OnAutoHolderListener listener) { dataMap.put(AutoHolder.LISTENER, listener); return setHolderToData(holderClass.hashCode(), holderClass, layoutRes, dataMap); } ///////////////////////////////////////////////////////////////////////// // set data bean ///////////////////////////////////////////////////////////////////////// private AutoRecyclerAdapter setDataObject(int index, int type, M bean, int spanSize) { AutoPackage autoPackage = new AutoPackage(type, bean, spanSize); packageList.add(index, autoPackage); dataList.add(index, bean); return this; } private AutoRecyclerAdapter setDataObject(int type, M bean, int spanSize) { AutoPackage autoPackage = new AutoPackage(type, bean, spanSize); packageList.add(autoPackage); dataList.add(bean); return this; } public AutoRecyclerAdapter setDataObject(Class holderClass, M bean) { return setDataObject(holderClass.hashCode(), bean, 0); } public AutoRecyclerAdapter setDataObjectIndex(int index, Class holderClass, M bean) { return setDataObject(index, holderClass.hashCode(), bean, 0); } public AutoRecyclerAdapter setDataObjectSpan(Class holderClass, M bean, int spanSize) { return setDataObject(holderClass.hashCode(), bean, spanSize); } public AutoRecyclerAdapter setDataObjectSpanIndex(int index, Class holderClass, M bean, int spanSize) { return setDataObject(index, holderClass.hashCode(), bean, spanSize); } public AutoRecyclerAdapter setDataObject(int key, M bean) { return setDataObject(key, bean, 0); } public AutoRecyclerAdapter setDataObjectIndex(int index, int key, M bean) { return setDataObject(index, key, bean, 0); } public AutoRecyclerAdapter setDataObjectSpan(int key, M bean, int spanSize) { return setDataObject(key, bean, spanSize); } public AutoRecyclerAdapter setDataObjectSpanIndex(int index, int key, M bean, int spanSize) { return setDataObject(index, key, bean, spanSize); } ///////////////////////////////////////////////////////////////////////// // remove data bean ///////////////////////////////////////////////////////////////////////// public AutoRecyclerAdapter removeDataObject(int index) { packageList.remove(index); dataList.remove(index); return this; } public AutoRecyclerAdapter removeDataObject(M bean) { int index = dataList.indexOf(bean); if (index >= 0) { removeDataObject(index); } return this; } ///////////////////////////////////////////////////////////////////////// // contains data bean ///////////////////////////////////////////////////////////////////////// public boolean containsDataObject(M bean) { boolean flag = false; for (Object object : dataList) { if (object == bean) { flag = true; break; } } return flag; } ///////////////////////////////////////////////////////////////////////// // set data list ///////////////////////////////////////////////////////////////////////// private AutoRecyclerAdapter setDataList(int type, List list, int spanSize) { if (list != null) { for (M bean : list) { AutoPackage autoPackage = new AutoPackage(type, bean, spanSize); packageList.add(autoPackage); dataList.add(bean); } } return this; } private AutoRecyclerAdapter setDataList(int index, int type, List list, int spanSize) { if (list != null) { for (int x = list.size() - 1; x >= 0; x--) { M bean = list.get(x); AutoPackage autoPackage = new AutoPackage(type, bean, spanSize); packageList.add(index, autoPackage); dataList.add(index, bean); } } return this; } public AutoRecyclerAdapter setDataList(Class holderClass, List list) { return setDataList(holderClass.hashCode(), list, 0); } public AutoRecyclerAdapter setDataListIndex(int index, Class holderClass, List list) { return setDataList(index, holderClass.hashCode(), list, 0); } public AutoRecyclerAdapter setDataListSpan(Class holderClass, List list, int spanSize) { return setDataList(holderClass.hashCode(), list, spanSize); } public AutoRecyclerAdapter setDataListSpanIndex(int index, Class holderClass, List list, int spanSize) { return setDataList(index, holderClass.hashCode(), list, spanSize); } public AutoRecyclerAdapter setDataList(int type, List list) { return setDataList(type, list, 0); } public AutoRecyclerAdapter setDataListIndex(int index, int type, List list) { return setDataList(index, type, list, 0); } public AutoRecyclerAdapter setDataListSpan(int type, List list, int spanSize) { return setDataList(type, list, spanSize); } public AutoRecyclerAdapter setDataListSpanIndex(int index, int type, List list, int spanSize) { return setDataList(index, type, list, spanSize); } ///////////////////////////////////////////////////////////////////////// // other ///////////////////////////////////////////////////////////////////////// public List getHolderList() { return holderList; } public List getAutoPackageList() { return packageList; } public List getDataList() { return dataList; } public AutoRecyclerAdapter clear() { packageList.clear(); dataList.clear(); return this; } public AutoRecyclerAdapter clearHolder() { holderList.clear(); return this; } }