PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/UltimateRecyclerView/ultimaterecyclerview/src/main/java/com/marshalchen/ultimaterecyclerview/expanx/LinearExpanxURVAdapter.java

https://gitlab.com/secApps/UltimateRecyclerView
Java | 340 lines | 248 code | 57 blank | 35 comment | 28 complexity | ab53e7b2bacc94cd6ee6f93cb4b7bc66 MD5 | raw file
  1. package com.marshalchen.ultimaterecyclerview.expanx;
  2. import android.content.Context;
  3. import android.support.annotation.LayoutRes;
  4. import android.support.v7.widget.RecyclerView;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
  9. import com.marshalchen.ultimaterecyclerview.expanx.Util.BaseViewHolder;
  10. import com.marshalchen.ultimaterecyclerview.expanx.Util.ChildVH;
  11. import com.marshalchen.ultimaterecyclerview.expanx.Util.ItemDataClickListener;
  12. import com.marshalchen.ultimaterecyclerview.expanx.Util.OnScrollToListener;
  13. import com.marshalchen.ultimaterecyclerview.expanx.Util.ParentVH;
  14. import com.marshalchen.ultimaterecyclerview.expanx.Util.child;
  15. import com.marshalchen.ultimaterecyclerview.expanx.Util.parent;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. /**
  19. * Created by hesk on 16/7/15.
  20. */
  21. public abstract class LinearExpanxURVAdapter<T extends ExpandableItemData, G extends parent<T>, H extends child<T>> extends UltimateViewAdapter {
  22. public class ExpandableViewTypes extends VIEW_TYPES {
  23. public static final int ITEM_TYPE_PARENT = 1026;
  24. public static final int ITEM_TYPE_CHILD = 1135;
  25. protected ExpandableViewTypes() {
  26. super();
  27. }
  28. }
  29. private Context mContext;
  30. private List<T> mDataSet;
  31. private List<OnScrollToListener> monScrollToListenerList = new ArrayList<>();
  32. private OnScrollToListener onScrollToListener;
  33. public static final String TAG = "expAdapter";
  34. protected int expandableBehavior = 0;
  35. public static final int EXPANDABLE_ITEMS = 1;
  36. public static final int EXPANDABLE_SYSTEM = 0;
  37. private boolean customObject;
  38. protected Context getContext() {
  39. return mContext;
  40. }
  41. protected List<T> getSet() {
  42. return mDataSet;
  43. }
  44. public void addOnScrollToListener(OnScrollToListener onScrollToListener) {
  45. this.monScrollToListenerList.add(onScrollToListener);
  46. }
  47. @Deprecated
  48. public void setOnScrollToListener(OnScrollToListener onScrollToListener) {
  49. this.onScrollToListener = onScrollToListener;
  50. }
  51. public LinearExpanxURVAdapter(Context context, final int clickhandler, final boolean customholder) {
  52. this(context, clickhandler);
  53. this.customObject = customholder;
  54. }
  55. public LinearExpanxURVAdapter(Context context, final int clickhandler) {
  56. this(context);
  57. expandableBehavior = clickhandler;
  58. }
  59. public LinearExpanxURVAdapter(Context context) {
  60. mContext = context;
  61. mDataSet = new ArrayList<>();
  62. customObject = false;
  63. }
  64. /**
  65. * please do work on this id the custom object is true
  66. *
  67. * @param parentview the inflated view
  68. * @return the actual parent holder
  69. */
  70. protected abstract G iniCustomParentHolder(View parentview);
  71. /**
  72. * please do work on this if the custom object is true
  73. *
  74. * @param childview the inflated view
  75. * @return the actual child holder
  76. */
  77. protected abstract H iniCustomChildHolder(View childview);
  78. private View initiateview(ViewGroup parent, final @LayoutRes int layout) {
  79. return LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
  80. }
  81. protected abstract int getLayoutResParent();
  82. protected abstract int getLayoutResChild();
  83. @Override
  84. public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  85. switch (viewType) {
  86. case ExpandableViewTypes.ITEM_TYPE_PARENT:
  87. return iniCustomParentHolder(initiateview(parent, getLayoutResParent()));
  88. case ExpandableViewTypes.ITEM_TYPE_CHILD:
  89. return iniCustomChildHolder(initiateview(parent, getLayoutResChild()));
  90. default:
  91. return null;
  92. }
  93. }
  94. private ItemDataClickListener getBehavior() {
  95. switch (expandableBehavior) {
  96. case EXPANDABLE_ITEMS:
  97. return imageSetLoadItems;
  98. default:
  99. return imageClickListener;
  100. }
  101. }
  102. @Override
  103. public RecyclerView.ViewHolder getViewHolder(View view) {
  104. return null;
  105. }
  106. @Override
  107. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  108. switch (getItemViewType(position)) {
  109. case ExpandableViewTypes.ITEM_TYPE_PARENT:
  110. ParentVH imageViewHolder = (ParentVH) holder;
  111. imageViewHolder.bindView(mDataSet.get(position), position, getBehavior());
  112. break;
  113. case ExpandableViewTypes.ITEM_TYPE_CHILD:
  114. ChildVH textViewHolder = (ChildVH) holder;
  115. textViewHolder.bindView(mDataSet.get(position), position);
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. @Override
  122. public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup) {
  123. return new BaseViewHolder(viewGroup);
  124. }
  125. private void triggerBoardCastEventScrollTo(final int n) {
  126. for (int i = 0; i < monScrollToListenerList.size(); i++) {
  127. OnScrollToListener m = monScrollToListenerList.get(i);
  128. m.scrollTo(n);
  129. }
  130. }
  131. private void triggerSingleEventScrollTo(final int n) {
  132. if (onScrollToListener != null) {
  133. onScrollToListener.scrollTo(n);
  134. }
  135. }
  136. protected abstract List<T> getChildrenByPath(String path, int depth, final int position);
  137. @Override
  138. public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
  139. return null;
  140. }
  141. @Override
  142. public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
  143. }
  144. @Override
  145. public int getItemCount() {
  146. return mDataSet.size();
  147. }
  148. /**
  149. * this is the only number coming from the data size
  150. *
  151. * @return the integer from the list count
  152. */
  153. @Override
  154. public int getAdapterItemCount() {
  155. return getItemCount();
  156. }
  157. @Override
  158. public long generateHeaderId(int i) {
  159. return 0;
  160. }
  161. private int getChildrenCount(T item) {
  162. List<Object> list = new ArrayList<>();
  163. printChild(item, list);
  164. return list.size();
  165. }
  166. private void printChild(Object item, List<Object> list) {
  167. list.add(item);
  168. if (item instanceof ExpandableItemData) {
  169. ExpandableItemData it = (ExpandableItemData) item;
  170. if (it.getChildren() != null) {
  171. for (int i = 0; i < it.getChildren().size(); i++) {
  172. printChild(it.getChildren().get(i), list);
  173. }
  174. }
  175. }
  176. }
  177. /**
  178. * 从position开始删除,删除
  179. *
  180. * @param position the count position
  181. * @param itemCount 删除的数目
  182. */
  183. protected void removeAll(int position, int itemCount) {
  184. for (int i = 0; i < itemCount; i++) {
  185. mDataSet.remove(position);
  186. }
  187. notifyItemRangeRemoved(position, itemCount);
  188. }
  189. /**
  190. * the current position from the uuid
  191. *
  192. * @param uuid the string in uuid
  193. * @return the int as the position
  194. */
  195. protected int getCurrentPosition(String uuid) {
  196. for (int i = 0; i < mDataSet.size(); i++) {
  197. if (uuid.equalsIgnoreCase(mDataSet.get(i).getUuid())) {
  198. return i;
  199. }
  200. }
  201. return -1;
  202. }
  203. @Override
  204. public int getItemViewType(int position) {
  205. return mDataSet.get(position).getType();
  206. }
  207. public void add(T text, int position) {
  208. mDataSet.add(position, text);
  209. notifyItemInserted(position);
  210. }
  211. public void addAll(List<T> list, int position) {
  212. mDataSet.addAll(position, list);
  213. notifyItemRangeInserted(position, list.size());
  214. }
  215. public void delete(int pos) {
  216. if (pos >= 0 && pos < mDataSet.size()) {
  217. if (mDataSet.get(pos).getType() == ExpandableViewTypes.ITEM_TYPE_PARENT
  218. && mDataSet.get(pos).isExpand()) {// 父组件并且子节点已经展开
  219. for (int i = 0; i < mDataSet.get(pos).getChildren().size() + 1; i++) {
  220. mDataSet.remove(pos);
  221. }
  222. notifyItemRangeRemoved(pos, mDataSet.get(pos).getChildren()
  223. .size() + 1);
  224. } else {// 孩子节点,或没有展开的父节点
  225. mDataSet.remove(pos);
  226. notifyItemRemoved(pos);
  227. }
  228. }
  229. }
  230. /**
  231. * the item click behavior and list item handler cases
  232. */
  233. private ItemDataClickListener imageSetLoadItems = new ItemDataClickListener<T>() {
  234. @Override
  235. public void onExpandChildren(T itemData) {
  236. int position = getCurrentPosition(itemData.getUuid());
  237. List<T> children = itemData.getChildren();
  238. if (children == null) {
  239. return;
  240. }
  241. addAll(children, position + 1); // 插入到点击点的下方
  242. triggerSingleEventScrollTo(position + 1);
  243. triggerBoardCastEventScrollTo(position + 1);
  244. }
  245. @Override
  246. public void onHideChildren(T itemData) {
  247. int position = getCurrentPosition(itemData.getUuid());
  248. List<T> children = itemData.getChildren();
  249. if (children == null) {
  250. return;
  251. }
  252. removeAll(position + 1, getChildrenCount(itemData) - 1);
  253. triggerSingleEventScrollTo(position);
  254. triggerBoardCastEventScrollTo(position);
  255. }
  256. };
  257. private ItemDataClickListener imageClickListener = new ItemDataClickListener<T>() {
  258. @Override
  259. public void onExpandChildren(T itemData) {
  260. int position = getCurrentPosition(itemData.getUuid());
  261. List<T> children = getChildrenByPath(itemData.getPath(), itemData.getTreeDepth(), position);
  262. if (children == null) {
  263. return;
  264. }
  265. addAll(children, position + 1);// 插入到点击点的下方
  266. itemData.setChildren(children);
  267. triggerSingleEventScrollTo(position + 1);
  268. triggerBoardCastEventScrollTo(position + 1);
  269. }
  270. @Override
  271. public void onHideChildren(T itemData) {
  272. int position = getCurrentPosition(itemData.getUuid());
  273. List<T> children = itemData.getChildren();
  274. if (children == null) {
  275. return;
  276. }
  277. removeAll(position + 1, getChildrenCount(itemData) - 1);
  278. triggerSingleEventScrollTo(position);
  279. triggerBoardCastEventScrollTo(position);
  280. itemData.setChildren(null);
  281. }
  282. };
  283. }