();
123 | if (childSize > 10) {
124 | group.setLoadMoreStatus(LoadMoreStatus.FINISH);
125 | } else {
126 | for (int i = 0; i < 5; i++)
127 | appendList.add(parentIndex + "" + (i + 1));
128 | group.getChildItemList().addAll(appendList);
129 | group.setLoadMoreStatus(LoadMoreStatus.INIT);
130 | }
131 | mAdapter.notifyChildItemRangeInserted(parentIndex, childSize, appendList.size());
132 | }
133 | }
134 | }, 2000);
135 | }
136 | });
137 |
138 | recyclerView.setAdapter(mAdapter);
139 | final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
140 | recyclerView.setLayoutManager(linearLayoutManager);
141 |
142 | findViewById(R.id.test_btn).setOnClickListener(new View.OnClickListener() {
143 | @Override
144 | public void onClick(View v) {
145 | num1.remove(0);
146 | mAdapter.notifyChildItemRemoved(0, 0);
147 | //if (!result) {
148 | // mAdapter.expandAllParents(1);
149 | //} else {
150 | // mAdapter.collapseAllParents();
151 | //}
152 | result = !result;
153 | }
154 | });
155 | }
156 |
157 | @Override
158 | protected void onSaveInstanceState(Bundle outState) {
159 | super.onSaveInstanceState(outState);
160 | mAdapter.onSaveInstanceState(outState);
161 | }
162 |
163 | @Override
164 | protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
165 | super.onRestoreInstanceState(savedInstanceState);
166 | mAdapter.onRestoreInstanceState(savedInstanceState);
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/expanddraglibrary/src/main/java/com/lighters/library/expanddrag/Adapter/ExpandDragRecyclerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.lighters.library.expanddrag.Adapter;
2 |
3 | import android.app.Activity;
4 | import android.content.ClipData;
5 | import android.content.Intent;
6 | import android.graphics.Bitmap;
7 | import android.graphics.Canvas;
8 | import android.graphics.Point;
9 | import android.graphics.drawable.BitmapDrawable;
10 | import android.graphics.drawable.Drawable;
11 | import android.os.Bundle;
12 | import android.os.Handler;
13 | import android.support.annotation.NonNull;
14 | import android.support.v7.widget.LinearLayoutManager;
15 | import android.support.v7.widget.RecyclerView;
16 | import android.view.DragEvent;
17 | import android.view.MotionEvent;
18 | import android.view.View;
19 | import android.view.ViewGroup;
20 | import com.lighters.library.expanddrag.Model.LoadMoreStatus;
21 | import com.lighters.library.expanddrag.Model.ParentListItem;
22 | import com.lighters.library.expanddrag.Model.ParentWrapper;
23 | import com.lighters.library.expanddrag.ViewHolder.ChildViewHolder;
24 | import com.lighters.library.expanddrag.ViewHolder.LoadMoreViewHolder;
25 | import com.lighters.library.expanddrag.ViewHolder.ParentViewHolder;
26 | import com.lighters.library.expanddrag.callback.DragSelectCallback;
27 | import com.lighters.library.expanddrag.callback.LoadMoreListener;
28 | import java.util.ArrayList;
29 | import java.util.Collections;
30 | import java.util.HashMap;
31 | import java.util.List;
32 |
33 | /**
34 | * RecyclerView.Adapter implementation that
35 | * adds the ability to expand and collapse list items.
36 | *
37 | * Changes should be notified through:
38 | * {@link #notifyParentItemInserted(int)}
39 | * {@link #notifyParentItemRemoved(int)}
40 | * {@link #notifyParentItemChanged(int)}
41 | * {@link #notifyParentItemRangeInserted(int, int)}
42 | * {@link #notifyChildItemInserted(int, int)}
43 | * {@link #notifyChildItemRemoved(int, int)}
44 | * {@link #notifyChildItemChanged(int, int)}
45 | * methods and not the notify methods of RecyclerView.Adapter.
46 | *
47 | * @author Ryan Brooks
48 | * @version 1.0
49 | * @since 5/27/2015
50 | */
51 | public abstract class ExpandDragRecyclerAdapter
53 | extends ExpandableRecyclerAdapter implements View.OnDragListener, View.OnLongClickListener, View.OnTouchListener {
54 |
55 | //private static final String TAG = ExpandDragRecyclerAdapter.class.getName() + "_tag";
56 |
57 | private static final int TYPE_LOAD_MORE = 2;
58 |
59 | private static final String FROM_POSITION = "from_position";
60 | private static final String FROM_PARENT_POSITION = "from_parent_position";
61 | private static final String FROM_CHILD_POSITION = "from_child_position";
62 | private static final String FROM_POSITION_DATA = "from_position_data";
63 |
64 | private DragSelectCallback mDragSelectCallback;
65 | private LoadMoreListener mLoadMoreListener;
66 |
67 | /**
68 | * 记录当前扩展的状态
69 | */
70 | private List mExpandedList = new ArrayList<>();
71 |
72 | /**
73 | * Primary constructor. Sets up {@link #mParentItemList} and {@link #mItemList}.
74 | *
75 | * Changes to {@link #mParentItemList} should be made through add/remove methods in
76 | * {@link ExpandableRecyclerAdapter}
77 | *
78 | * @param parentItemList List of all {@link ParentListItem} objects to be
79 | * displayed in the RecyclerView that this
80 | * adapter is linked to
81 | */
82 | public ExpandDragRecyclerAdapter(@NonNull List extends ParentListItem> parentItemList) {
83 | super(parentItemList);
84 | }
85 |
86 | /**
87 | * Implementation of Adapter.onCreateViewHolder(ViewGroup, int)
88 | * that determines if the list item is a parent or a child and calls through
89 | * to the appropriate implementation of either {@link #onCreateParentViewHolder(ViewGroup)}
90 | * or {@link #onCreateChildViewHolder(ViewGroup)}.
91 | *
92 | * @param viewGroup The {@link ViewGroup} into which the new {@link View}
93 | * will be added after it is bound to an adapter position.
94 | * @param viewType The view type of the new {@code android.view.View}.
95 | * @return A new RecyclerView.ViewHolder
96 | * that holds a {@code android.view.View} of the given view type.
97 | */
98 | @Override
99 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
100 | if (viewType == TYPE_LOAD_MORE) {
101 | LoadMoreViewHolder loadMoreViewHolder = onCreateLoadMoreViewHolder(viewGroup);
102 | loadMoreViewHolder.setLoadMoreCallback(mLoadMoreCallback);
103 | return loadMoreViewHolder;
104 | } else {
105 | return super.onCreateViewHolder(viewGroup, viewType);
106 | }
107 | }
108 |
109 | /**
110 | * Implementation of Adapter.onBindViewHolder(RecyclerView.ViewHolder, int)
111 | * that determines if the list item is a parent or a child and calls through
112 | * to the appropriate implementation of either
113 | * {@link #onBindParentViewHolder(ParentViewHolder, int, ParentListItem)}
114 | * or {@link #onBindChildViewHolder(ChildViewHolder, int, Object)}.
115 | *
116 | * @param holder The RecyclerView.ViewHolder to bind data to
117 | * @param position The index in the list at which to bind
118 | * @throws IllegalStateException if the item in the list is either null or
119 | * not of type {@link ParentListItem}
120 | */
121 | @Override
122 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
123 | Object listItem = getListItem(position);
124 | if (listItem instanceof ParentWrapper) {
125 | PVH parentViewHolder = (PVH) holder;
126 |
127 | if (parentViewHolder.shouldItemViewClickToggleExpansion()) {
128 | parentViewHolder.setMainItemClickToExpand();
129 | }
130 | parentViewHolder.itemView.setTag(position);
131 | parentViewHolder.itemView.setOnDragListener(this);
132 | ParentWrapper parentWrapper = (ParentWrapper) listItem;
133 | parentViewHolder.setExpanded(parentWrapper.isExpanded());
134 | onBindParentViewHolder(parentViewHolder, position, parentWrapper.getParentListItem());
135 | } else if (listItem != null && listItem instanceof LoadMoreStatus) {
136 | LVH loadMoreViewHolder = (LVH) holder;
137 | loadMoreViewHolder.update((LoadMoreStatus) listItem);
138 | onBindLoadMoreViewHolder((LVH) holder, position, getParentIndex(position), listItem);
139 | } else if (listItem == null) {
140 | throw new IllegalStateException("Incorrect ViewHolder found");
141 | } else {
142 | holder.itemView.setTag(position);
143 | holder.itemView.setOnLongClickListener(this);
144 | holder.itemView.setOnTouchListener(this);
145 | onBindChildViewHolder((CVH) holder, position, listItem);
146 | }
147 | }
148 |
149 | /**
150 | * Create the load more view holder
151 | */
152 | public abstract LVH onCreateLoadMoreViewHolder(ViewGroup viewGroup);
153 |
154 | /**
155 | * Bind the load more view holder
156 | */
157 | public abstract void onBindLoadMoreViewHolder(LVH viewHolder, int position, int parentIndex, Object object);
158 |
159 | /**
160 | * Gets the view type of the item at the given position.
161 | *
162 | * @param position The index in the list to get the view type of
163 | * @return {@value #TYPE_PARENT} for {@link ParentListItem} and {@value #TYPE_CHILD}
164 | * for child list items
165 | * @throws IllegalStateException if the item at the given position in the list is null
166 | */
167 | @Override
168 | public int getItemViewType(int position) {
169 | Object listItem = getListItem(position);
170 | if (listItem instanceof LoadMoreStatus) {
171 | return TYPE_LOAD_MORE;
172 | } else {
173 | return super.getItemViewType(position);
174 | }
175 | }
176 |
177 | /**
178 | * 设置拖拽选中回调
179 | */
180 | public void setDragSelectCallback(DragSelectCallback dragSelectCallback) {
181 | mDragSelectCallback = dragSelectCallback;
182 | }
183 |
184 | /**
185 | * 设置加载更多地回调
186 | */
187 | public void setLoadMoreListener(LoadMoreListener loadMoreListener) {
188 | mLoadMoreListener = loadMoreListener;
189 | }
190 |
191 | /**
192 | * Expands all parents in the list.
193 | */
194 | public void expandAllParents(int fromParenIndex) {
195 | if (fromParenIndex >= 0 && fromParenIndex < mParentItemList.size()) {
196 | expandParent(fromParenIndex);
197 | }
198 | int scrollPosition = 0;
199 | ParentListItem parentListItem = null;
200 | for (int i = 0; i < mParentItemList.size(); i++) {
201 | if (i < fromParenIndex) {
202 | expandParent(i);
203 | parentListItem = mParentItemList.get(i);
204 | int childCount = parentListItem.getChildItemList().size();
205 | if (parentListItem.isLoadMore()) {
206 | childCount += 1;
207 | }
208 | scrollPosition += childCount + i;
209 | scrollToPosition(scrollPosition);
210 | } else {
211 | expandParent(i);
212 | }
213 | }
214 | }
215 |
216 | /**
217 | * expand the specific parent items.
218 | *
219 | * @param list the parent item list to be expand.
220 | * @param selectItem current selected items.
221 | */
222 | public void expandParentItems(List list, int selectItem) {
223 | if (list != null && list.size() > 0) {
224 | if (!list.contains(selectItem)) {
225 | list.add(selectItem);
226 | }
227 | Collections.sort(list);
228 | expandParent(selectItem);
229 | int scrollPosition = 0;
230 | ParentListItem parentListItem = null;
231 | for (Integer i : list) {
232 | if (i < selectItem) {
233 | expandParent(i);
234 | parentListItem = mParentItemList.get(i);
235 | int childCount = parentListItem.getChildItemList().size();
236 | if (parentListItem.isLoadMore()) {
237 | childCount += 1;
238 | }
239 | scrollPosition += childCount + i;
240 | scrollToPosition(scrollPosition);
241 | } else {
242 | expandParent(i);
243 | }
244 | }
245 | }
246 | }
247 |
248 | private void scrollToPosition(int position) {
249 | RecyclerView.LayoutManager layoutManager = null;
250 | for (RecyclerView recyclerView : mAttachedRecyclerViewPool) {
251 | layoutManager = recyclerView.getLayoutManager();
252 | if (layoutManager instanceof LinearLayoutManager) {
253 | ((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(position, 0);
254 | }
255 | }
256 | }
257 |
258 | /**
259 | * Fetches the expandable state map from the saved instance state {@link Bundle}
260 | * and restores the expanded states of all of the list items.
261 | *
262 | * Should be called from {@link Activity#onRestoreInstanceState(Bundle)} in
263 | * the {@link Activity} that hosts the RecyclerView that this
264 | * {@link ExpandDragRecyclerAdapter} is attached to.
265 | *
266 | * Assumes that the list of parent list items is the same as when the saved
267 | * instance state was stored.
268 | *
269 | * @param savedInstanceState The {@code Bundle} from which the expanded
270 | * state map is loaded
271 | */
272 | @Override
273 | public void onRestoreInstanceState(Bundle savedInstanceState) {
274 | if (savedInstanceState == null || !savedInstanceState.containsKey(EXPANDED_STATE_MAP)) {
275 | return;
276 | }
277 |
278 | HashMap expandedStateMap =
279 | (HashMap) savedInstanceState.getSerializable(EXPANDED_STATE_MAP);
280 | if (expandedStateMap == null) {
281 | return;
282 | }
283 |
284 | List