Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. package com.hua.rcvtest;
  2.  
  3. import android.support.annotation.NonNull;
  4. import android.support.v7.widget.LinearLayoutManager;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.View;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public abstract class GroupAdapter<R,C> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  12.  
  13. public static final int TYPE_HEADER = 0;
  14. private List<R> items;
  15. private ArrayList<GroupHeader<C>> headers;
  16. protected ArrayList<Object> itemWithHeaders;
  17. private LinearLayoutManager linearLayoutManager;
  18. private ActionChange<C> actionChange;
  19.  
  20. interface ActionChange<C>{
  21. void onChanged(C obj);
  22. }
  23.  
  24. public void setActionChange(ActionChange<C> actionChange) {
  25. this.actionChange = actionChange;
  26. }
  27.  
  28. public GroupAdapter(List<R> items) {
  29. this.items = items;
  30. initHeader();
  31. }
  32.  
  33. public void setupRecyclerView(RecyclerView rcv, final View placeHolder){
  34. linearLayoutManager = new LinearLayoutManager(rcv.getContext());
  35. rcv.setLayoutManager(linearLayoutManager);
  36. rcv.setAdapter(this);
  37. if (actionChange!=null && !headers.isEmpty()){
  38. actionChange.onChanged(headers.get(0).groupObj);
  39. }
  40. rcv.addOnScrollListener(new RecyclerView.OnScrollListener() {
  41. @Override
  42. public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
  43. super.onScrolled(recyclerView, dx, dy);
  44. int firstHeaderIndex = findFirstHeader(linearLayoutManager.findFirstVisibleItemPosition(),
  45. linearLayoutManager.findLastVisibleItemPosition());
  46. if (firstHeaderIndex == -1){
  47. return;
  48. }
  49. View firstHederView = linearLayoutManager.findViewByPosition(firstHeaderIndex);
  50. // Log.i("fuck", "view="+firstHederView.getY());
  51. if (firstHederView == null){
  52. return;
  53. }
  54. float ty = firstHederView.getY();
  55. float h = placeHolder.getHeight();
  56. float perY = placeHolder.getY();
  57. if (ty<=0){
  58. placeHolder.setY(0);
  59. }else if(ty <= h){
  60. placeHolder.setY(ty-h);
  61. }else {
  62. placeHolder.setY(0);
  63. }
  64. if (perY!=0 && placeHolder.getY() ==0){
  65. // Log.e("fuck", "onShow: ");
  66. GroupHeader<C> header= getNowHeader(linearLayoutManager.findFirstVisibleItemPosition());
  67. if (actionChange!=null && header!=null){
  68. actionChange.onChanged(header.groupObj);
  69. }
  70.  
  71. }
  72. }
  73. });
  74.  
  75. }
  76.  
  77. public void replaceData(List<R> items){
  78. this.items.clear();
  79. this.items.addAll(items);
  80. initHeader();
  81. notifyDataSetChanged();
  82. }
  83.  
  84. public void addDatas(List<R> items){
  85. this.items.addAll(items);
  86. C perDate = headers.get(headers.size()-1).groupObj;
  87. for (int i = 0; i < items.size(); i++) {
  88. R temp = items.get(i);
  89. if (!compareGroupObj(perDate,temp)){
  90. perDate = objGetGroupVar(temp);
  91. GroupHeader<C> header = new GroupHeader<>(perDate, i + headers.size());
  92. headers.add(header);
  93. itemWithHeaders.add(header);
  94. }
  95. itemWithHeaders.add(temp);
  96. }
  97. }
  98.  
  99.  
  100. private void initHeader() {
  101. headers = new ArrayList<>();
  102. itemWithHeaders = new ArrayList<>();
  103. C perDate = null;
  104. for (int i = 0; i < items.size(); i++) {
  105. R temp = items.get(i);
  106. if (!compareGroupObj(perDate,temp)){
  107. perDate = objGetGroupVar(temp);
  108. GroupHeader<C> header = new GroupHeader<>(perDate, i + headers.size());
  109. headers.add(header);
  110. itemWithHeaders.add(header);
  111. }
  112. itemWithHeaders.add(temp);
  113. }
  114. }
  115.  
  116. private int findFirstHeader(int start,int end){
  117. for (int i = start; i <= end; i++) {
  118. Object temp = itemWithHeaders.get(i);
  119. if (temp instanceof GroupHeader){
  120. return i;
  121. }
  122. }
  123. return -1;
  124. }
  125.  
  126. private GroupHeader<C> getNowHeader(int start){
  127. for (int i = headers.size() - 1; i >= 0; i--) {
  128. GroupHeader temp = headers.get(i);
  129. if (temp.getStartIndex() <= start){
  130. return headers.get(i);
  131. }
  132. }
  133. return null;
  134. }
  135.  
  136.  
  137.  
  138. /**
  139. * 比较分组 ,数据是否属于该分组
  140. * @param that
  141. * @param obj
  142. * @return
  143. */
  144. abstract boolean compareGroupObj(C that,R obj);
  145.  
  146. /**
  147. * 从数据获取比较用的变量
  148. * @param obj
  149. * @return
  150. */
  151. abstract C objGetGroupVar(R obj);
  152.  
  153.  
  154. @Override
  155. public int getItemViewType(int position) {
  156. Object temp =itemWithHeaders.get(position);
  157. if (temp instanceof GroupHeader){
  158. return TYPE_HEADER;
  159. }
  160. return 1;
  161. }
  162.  
  163. @Override
  164. public int getItemCount() {
  165. return itemWithHeaders.size();
  166. }
  167. public static class GroupHeader<C>{
  168. private C groupObj;
  169. private int startIndex;
  170.  
  171. public GroupHeader(C groupObj, int startIndex) {
  172. this.groupObj = groupObj;
  173. this.startIndex = startIndex;
  174. }
  175.  
  176. public C getGroupObj() {
  177. return groupObj;
  178. }
  179.  
  180. public GroupHeader<C> setGroupObj(C groupObj) {
  181. this.groupObj = groupObj;
  182. return this;
  183. }
  184.  
  185. public int getStartIndex() {
  186. return startIndex;
  187. }
  188.  
  189. public GroupHeader<C> setStartIndex(int startIndex) {
  190. this.startIndex = startIndex;
  191. return this;
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement