T getOwnerAdapter(){
56 | RecyclerView recyclerView = getOwnerRecyclerView();
57 | return recyclerView == null?null: (T) recyclerView.getAdapter();
58 | }
59 |
60 | @Nullable
61 | protected RecyclerView getOwnerRecyclerView(){
62 | try {
63 | Field field = RecyclerView.ViewHolder.class.getDeclaredField("mOwnerRecyclerView");
64 | field.setAccessible(true);
65 | return (RecyclerView) field.get(this);
66 | } catch (NoSuchFieldException e) {
67 | } catch (IllegalAccessException e) {
68 | }
69 | return null;
70 | }
71 |
72 | }
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/java/com/jude/easyrecyclerview/adapter/DefaultEventDelegate.java:
--------------------------------------------------------------------------------
1 | package com.jude.easyrecyclerview.adapter;
2 |
3 | import android.util.Log;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.FrameLayout;
8 |
9 | import com.jude.easyrecyclerview.EasyRecyclerView;
10 |
11 | /**
12 | * Created by Mr.Jude on 2015/8/18.
13 | */
14 | public class DefaultEventDelegate implements EventDelegate {
15 | private RecyclerArrayAdapter adapter;
16 | private EventFooter footer ;
17 |
18 | private RecyclerArrayAdapter.OnMoreListener onMoreListener;
19 | private RecyclerArrayAdapter.OnNoMoreListener onNoMoreListener;
20 | private RecyclerArrayAdapter.OnErrorListener onErrorListener;
21 |
22 | private boolean hasData = false;
23 | private boolean isLoadingMore = false;
24 |
25 | private boolean hasMore = false;
26 | private boolean hasNoMore = false;
27 | private boolean hasError = false;
28 |
29 | private int status = STATUS_INITIAL;
30 | private static final int STATUS_INITIAL = 291;
31 | private static final int STATUS_MORE = 260;
32 | private static final int STATUS_NOMORE = 408;
33 | private static final int STATUS_ERROR = 732;
34 |
35 | public DefaultEventDelegate(RecyclerArrayAdapter adapter) {
36 | this.adapter = adapter;
37 | footer = new EventFooter();
38 | adapter.addFooter(footer);
39 | }
40 |
41 | public void onMoreViewShowed() {
42 | log("onMoreViewShowed");
43 | if (!isLoadingMore&& onMoreListener !=null){
44 | isLoadingMore = true;
45 | onMoreListener.onMoreShow();
46 | }
47 | }
48 |
49 | public void onMoreViewClicked() {
50 | if (onMoreListener !=null) onMoreListener.onMoreClick();
51 | }
52 |
53 | public void onErrorViewShowed() {
54 | if (onErrorListener!=null)onErrorListener.onErrorShow();
55 | }
56 |
57 | public void onErrorViewClicked() {
58 | if (onErrorListener!=null)onErrorListener.onErrorClick();
59 | }
60 |
61 | public void onNoMoreViewShowed() {
62 | if (onNoMoreListener!=null)onNoMoreListener.onNoMoreShow();
63 | }
64 |
65 | public void onNoMoreViewClicked() {
66 | if (onNoMoreListener!=null)onNoMoreListener.onNoMoreClick();
67 | }
68 |
69 | //-------------------5个状态触发事件-------------------
70 | @Override
71 | public void addData(int length) {
72 | log("addData" + length);
73 | if (hasMore){
74 | if (length == 0){
75 | //当添加0个时,认为已结束加载到底
76 | if (status==STATUS_INITIAL || status == STATUS_MORE){
77 | footer.showNoMore();
78 | status = STATUS_NOMORE;
79 | }
80 | }else {
81 | //当Error或初始时。添加数据,如果有More则还原。
82 | footer.showMore();
83 | status = STATUS_MORE;
84 | hasData = true;
85 | }
86 | }else{
87 | if (hasNoMore){
88 | footer.showNoMore();
89 | status = STATUS_NOMORE;
90 | }
91 | }
92 | isLoadingMore = false;
93 | }
94 |
95 | @Override
96 | public void clear() {
97 | log("clear");
98 | hasData = false;
99 | status = STATUS_INITIAL;
100 | footer.hide();
101 | isLoadingMore = false;
102 | }
103 |
104 | @Override
105 | public void stopLoadMore() {
106 | log("stopLoadMore");
107 | footer.showNoMore();
108 | status = STATUS_NOMORE;
109 | isLoadingMore = false;
110 | }
111 |
112 | @Override
113 | public void pauseLoadMore() {
114 | log("pauseLoadMore");
115 | footer.showError();
116 | status = STATUS_ERROR;
117 | isLoadingMore = false;
118 | }
119 |
120 | @Override
121 | public void resumeLoadMore() {
122 | isLoadingMore = false;
123 | footer.showMore();
124 | status = STATUS_MORE;
125 | onMoreViewShowed();
126 | }
127 |
128 | //-------------------3种View设置-------------------
129 |
130 | @Override
131 | public void setMore(View view, RecyclerArrayAdapter.OnMoreListener listener) {
132 | this.footer.setMoreView(view);
133 | this.onMoreListener = listener;
134 | hasMore = true;
135 | // 为了处理setMore之前就添加了数据的情况
136 | if (adapter.getCount()>0){
137 | addData(adapter.getCount());
138 | }
139 | log("setMore");
140 | }
141 |
142 | @Override
143 | public void setNoMore(View view, RecyclerArrayAdapter.OnNoMoreListener listener) {
144 | this.footer.setNoMoreView(view);
145 | this.onNoMoreListener = listener;
146 | hasNoMore = true;
147 | log("setNoMore");
148 | }
149 |
150 | @Override
151 | public void setErrorMore(View view, RecyclerArrayAdapter.OnErrorListener listener) {
152 | this.footer.setErrorView(view);
153 | this.onErrorListener = listener;
154 | hasError = true;
155 | log("setErrorMore");
156 | }
157 |
158 | @Override
159 | public void setMore(int res, RecyclerArrayAdapter.OnMoreListener listener) {
160 | this.footer.setMoreViewRes(res);
161 | this.onMoreListener = listener;
162 | hasMore = true;
163 | // 为了处理setMore之前就添加了数据的情况
164 | if (adapter.getCount()>0){
165 | addData(adapter.getCount());
166 | }
167 | log("setMore");
168 | }
169 |
170 | @Override
171 | public void setNoMore(int res, RecyclerArrayAdapter.OnNoMoreListener listener) {
172 | this.footer.setNoMoreViewRes(res);
173 | this.onNoMoreListener = listener;
174 | hasNoMore = true;
175 | log("setNoMore");
176 | }
177 |
178 | @Override
179 | public void setErrorMore(int res, RecyclerArrayAdapter.OnErrorListener listener) {
180 | this.footer.setErrorViewRes(res);
181 | this.onErrorListener = listener;
182 | hasError = true;
183 | log("setErrorMore");
184 | }
185 |
186 | private class EventFooter implements RecyclerArrayAdapter.ItemView {
187 | private View moreView = null;
188 | private View noMoreView = null;
189 | private View errorView = null;
190 | private int moreViewRes = 0;
191 | private int noMoreViewRes = 0;
192 | private int errorViewRes = 0;
193 |
194 | private int flag = Hide;
195 | public static final int Hide = 0;
196 | public static final int ShowMore = 1;
197 | public static final int ShowError = 2;
198 | public static final int ShowNoMore = 3;
199 |
200 | public boolean skipError = false;
201 | public boolean skipNoMore = false;
202 |
203 | public EventFooter(){
204 | }
205 |
206 | @Override
207 | public View onCreateView(ViewGroup parent) {
208 | log("onCreateView");
209 | return refreshStatus(parent);
210 | }
211 |
212 | @Override
213 | public void onBindView(View headerView) {
214 | log("onBindView");
215 | headerView.post(new Runnable() {
216 | @Override
217 | public void run() {
218 | switch (flag){
219 | case ShowMore:
220 | onMoreViewShowed();
221 | break;
222 | case ShowNoMore:
223 | if (!skipNoMore)onNoMoreViewShowed();skipNoMore = false;
224 | break;
225 | case ShowError:
226 | if (!skipError) onErrorViewShowed();skipError = false;
227 | break;
228 | }
229 | }
230 | });
231 | }
232 |
233 | public View refreshStatus(ViewGroup parent){
234 | View view = null;
235 | switch (flag){
236 | case ShowMore:
237 | if (moreView!=null) view = moreView;
238 | else if (moreViewRes!=0)view = LayoutInflater.from(parent.getContext()).inflate(moreViewRes,parent,false);
239 | if (view!=null)view.setOnClickListener(new View.OnClickListener() {
240 | @Override
241 | public void onClick(View v) {
242 | onMoreViewClicked();
243 | }
244 | });
245 | break;
246 | case ShowError:
247 | if (errorView!=null) view = errorView;
248 | else if (errorViewRes!=0)view = LayoutInflater.from(parent.getContext()).inflate(errorViewRes,parent,false);
249 | if (view!=null)view.setOnClickListener(new View.OnClickListener() {
250 | @Override
251 | public void onClick(View v) {
252 | onErrorViewClicked();
253 | }
254 | });
255 | break;
256 | case ShowNoMore:
257 | if (noMoreView!=null) view = noMoreView;
258 | else if (noMoreViewRes!=0)view = LayoutInflater.from(parent.getContext()).inflate(noMoreViewRes,parent,false);
259 | if (view!=null)view.setOnClickListener(new View.OnClickListener() {
260 | @Override
261 | public void onClick(View v) {
262 | onNoMoreViewClicked();
263 | }
264 | });
265 | break;
266 | }
267 | if (view == null)view = new FrameLayout(parent.getContext());
268 | return view;
269 | }
270 |
271 | public void showError(){
272 | log("footer showError");
273 | skipError = true;
274 | flag = ShowError;
275 | if (adapter.getItemCount()>0)
276 | adapter.notifyItemChanged(adapter.getItemCount()-1);
277 | }
278 | public void showMore(){
279 | log("footer showMore");
280 | flag = ShowMore;
281 | if (adapter.getItemCount()>0)
282 | adapter.notifyItemChanged(adapter.getItemCount()-1);
283 | }
284 | public void showNoMore(){
285 | log("footer showNoMore");
286 | skipNoMore = true;
287 | flag = ShowNoMore;
288 | if (adapter.getItemCount()>0)
289 | adapter.notifyItemChanged(adapter.getItemCount()-1);
290 | }
291 |
292 | //初始化
293 | public void hide(){
294 | log("footer hide");
295 | flag = Hide;
296 | if (adapter.getItemCount()>0)
297 | adapter.notifyItemChanged(adapter.getItemCount()-1);
298 | }
299 |
300 | public void setMoreView(View moreView) {
301 | this.moreView = moreView;
302 | this.moreViewRes = 0;
303 | }
304 |
305 | public void setNoMoreView(View noMoreView) {
306 | this.noMoreView = noMoreView;
307 | this.noMoreViewRes = 0;
308 | }
309 |
310 | public void setErrorView(View errorView) {
311 | this.errorView = errorView;
312 | this.errorViewRes = 0;
313 | }
314 |
315 | public void setMoreViewRes(int moreViewRes) {
316 | this.moreView = null;
317 | this.moreViewRes = moreViewRes;
318 | }
319 |
320 | public void setNoMoreViewRes(int noMoreViewRes) {
321 | this.noMoreView = null;
322 | this.noMoreViewRes = noMoreViewRes;
323 | }
324 |
325 | public void setErrorViewRes(int errorViewRes) {
326 | this.errorView = null;
327 | this.errorViewRes = errorViewRes;
328 | }
329 |
330 | @Override
331 | public int hashCode() {
332 | return flag+13589;
333 | }
334 | }
335 |
336 |
337 |
338 | private static void log(String content){
339 | if (EasyRecyclerView.DEBUG){
340 | Log.i(EasyRecyclerView.TAG,content);
341 | }
342 | }
343 | }
344 |
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/java/com/jude/easyrecyclerview/adapter/EventDelegate.java:
--------------------------------------------------------------------------------
1 | package com.jude.easyrecyclerview.adapter;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by Mr.Jude on 2015/8/18.
7 | */
8 | public interface EventDelegate {
9 | void addData(int length);
10 | void clear();
11 |
12 | void stopLoadMore();
13 | void pauseLoadMore();
14 | void resumeLoadMore();
15 |
16 | void setMore(View view,RecyclerArrayAdapter.OnMoreListener listener);
17 | void setNoMore(View view, RecyclerArrayAdapter.OnNoMoreListener listener);
18 | void setErrorMore(View view, RecyclerArrayAdapter.OnErrorListener listener);
19 | void setMore(int res, RecyclerArrayAdapter.OnMoreListener listener);
20 | void setNoMore(int res, RecyclerArrayAdapter.OnNoMoreListener listener);
21 | void setErrorMore(int res, RecyclerArrayAdapter.OnErrorListener listener);
22 | }
23 |
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/java/com/jude/easyrecyclerview/adapter/FixDataObserver.java:
--------------------------------------------------------------------------------
1 | package com.jude.easyrecyclerview.adapter;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 |
5 | /**
6 | * Created by zhuchenxi on 2016/12/22.
7 | */
8 |
9 | public class FixDataObserver extends RecyclerView.AdapterDataObserver {
10 |
11 | private RecyclerView recyclerView;
12 |
13 | public FixDataObserver(RecyclerView recyclerView) {
14 | this.recyclerView = recyclerView;
15 | }
16 |
17 |
18 |
19 | @Override
20 | public void onItemRangeInserted(int positionStart, int itemCount) {
21 | if (recyclerView.getAdapter() instanceof RecyclerArrayAdapter) {
22 | RecyclerArrayAdapter adapter = (RecyclerArrayAdapter) recyclerView.getAdapter();
23 | if (adapter.getFooterCount() > 0 && adapter.getCount() == itemCount) {
24 | recyclerView.scrollToPosition(0);
25 | }
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/java/com/jude/easyrecyclerview/adapter/RecyclerArrayAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2006 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jude.easyrecyclerview.adapter;
18 |
19 | import android.content.Context;
20 | import android.support.v7.widget.GridLayoutManager;
21 | import android.support.v7.widget.RecyclerView;
22 | import android.support.v7.widget.StaggeredGridLayoutManager;
23 | import android.util.Log;
24 | import android.view.View;
25 | import android.view.ViewGroup;
26 |
27 | import com.jude.easyrecyclerview.EasyRecyclerView;
28 |
29 | import java.util.ArrayList;
30 | import java.util.Arrays;
31 | import java.util.Collection;
32 | import java.util.Collections;
33 | import java.util.Comparator;
34 | import java.util.List;
35 |
36 | /**
37 | * A concrete BaseAdapter that is backed by an array of arbitrary
38 | * objects. By default this class expects that the provided resource id references
39 | * a single TextView. If you want to use a more complex layout, use the constructors that
40 | * also takes a field id. That field id should reference a TextView in the larger layout
41 | * resource.
42 | *
43 | * However the TextView is referenced, it will be filled with the toString() of each object in
44 | * the array. You can add lists or arrays of custom objects. Override the toString() method
45 | * of your objects to determine what text will be displayed for the item in the list.
46 | *
47 | *
To use something other than TextViews for the array display, for instance, ImageViews,
48 | * or to have some of data besides toString() results fill the views,
49 | */
50 | abstract public class RecyclerArrayAdapter extends RecyclerView.Adapter {
51 | /**
52 | * Contains the list of objects that represent the data of this ArrayAdapter.
53 | * The content of this list is referred to as "the array" in the documentation.
54 | */
55 | protected List mObjects;
56 | protected EventDelegate mEventDelegate;
57 | protected ArrayList headers = new ArrayList<>();
58 | protected ArrayList footers = new ArrayList<>();
59 |
60 | protected OnItemClickListener mItemClickListener;
61 | protected OnItemLongClickListener mItemLongClickListener;
62 |
63 | protected RecyclerView mRecyclerView;
64 |
65 | public interface ItemView {
66 | View onCreateView(ViewGroup parent);
67 | void onBindView(View headerView);
68 | }
69 | public interface OnLoadMoreListener{
70 | void onLoadMore();
71 | }
72 | public interface OnMoreListener{
73 | void onMoreShow();
74 | void onMoreClick();
75 | }
76 | public interface OnNoMoreListener{
77 | void onNoMoreShow();
78 | void onNoMoreClick();
79 | }
80 | public interface OnErrorListener{
81 | void onErrorShow();
82 | void onErrorClick();
83 | }
84 |
85 | public class GridSpanSizeLookup extends GridLayoutManager.SpanSizeLookup{
86 | private int mMaxCount;
87 | public GridSpanSizeLookup(int maxCount){
88 | this.mMaxCount = maxCount;
89 | }
90 | @Override
91 | public int getSpanSize(int position) {
92 | if (headers.size()!=0){
93 | if (position= 0) {
98 | return mMaxCount;
99 | }
100 | }
101 | return 1;
102 | }
103 | }
104 |
105 | public GridSpanSizeLookup obtainGridSpanSizeLookUp(int maxCount){
106 | return new GridSpanSizeLookup(maxCount);
107 | }
108 |
109 | /**
110 | * Lock used to modify the content of {@link #mObjects}. Any write operation
111 | * performed on the array should be synchronized on this lock.
112 | */
113 | private final Object mLock = new Object();
114 |
115 |
116 | /**
117 | * Indicates whether or not {@link #notifyDataSetChanged()} must be called whenever
118 | * {@link #mObjects} is modified.
119 | */
120 | private boolean mNotifyOnChange = true;
121 |
122 | private Context mContext;
123 |
124 |
125 | /**
126 | * Constructor
127 | *
128 | * @param context The current context.
129 | */
130 | public RecyclerArrayAdapter(Context context) {
131 | init(context, new ArrayList());
132 | }
133 |
134 |
135 | /**
136 | * Constructor
137 | *
138 | * @param context The current context.
139 | * @param objects The objects to represent in the ListView.
140 | */
141 | public RecyclerArrayAdapter(Context context, T[] objects) {
142 | init(context, Arrays.asList(objects));
143 | }
144 |
145 | /**
146 | * Constructor
147 | *
148 | * @param context The current context.
149 | * @param objects The objects to represent in the ListView.
150 | */
151 | public RecyclerArrayAdapter(Context context, List objects) {
152 | init(context, objects);
153 | }
154 |
155 |
156 | private void init(Context context , List objects) {
157 | mContext = context;
158 | mObjects = new ArrayList<>(objects);
159 | }
160 |
161 |
162 | public void stopMore(){
163 | if (mEventDelegate == null)throw new NullPointerException("You should invoking setLoadMore() first");
164 | mEventDelegate.stopLoadMore();
165 | }
166 |
167 | public void pauseMore(){
168 | if (mEventDelegate == null)throw new NullPointerException("You should invoking setLoadMore() first");
169 | mEventDelegate.pauseLoadMore();
170 | }
171 |
172 | public void resumeMore(){
173 | if (mEventDelegate == null)throw new NullPointerException("You should invoking setLoadMore() first");
174 | mEventDelegate.resumeLoadMore();
175 | }
176 |
177 |
178 | public void addHeader(ItemView view){
179 | if (view==null)throw new NullPointerException("ItemView can't be null");
180 | headers.add(view);
181 | notifyItemInserted(headers.size()-1);
182 | }
183 |
184 | public void addFooter(ItemView view){
185 | if (view==null)throw new NullPointerException("ItemView can't be null");
186 | footers.add(view);
187 | notifyItemInserted(headers.size()+getCount()+footers.size()-1);
188 | }
189 |
190 | public void removeAllHeader(){
191 | int count = headers.size();
192 | headers.clear();
193 | notifyItemRangeRemoved(0,count);
194 | }
195 |
196 | public void removeAllFooter(){
197 | int count = footers.size();
198 | footers.clear();
199 | notifyItemRangeRemoved(headers.size()+getCount(),count);
200 | }
201 |
202 | public ItemView getHeader(int index){
203 | return headers.get(index);
204 | }
205 |
206 | public ItemView getFooter(int index){
207 | return footers.get(index);
208 | }
209 |
210 | public int getHeaderCount(){return headers.size();}
211 |
212 | public int getFooterCount(){return footers.size();}
213 |
214 | public void removeHeader(ItemView view){
215 | int position = headers.indexOf(view);
216 | headers.remove(view);
217 | notifyItemRemoved(position);
218 | }
219 |
220 | public void removeFooter(ItemView view){
221 | int position = headers.size()+getCount()+footers.indexOf(view);
222 | footers.remove(view);
223 | notifyItemRemoved(position);
224 | }
225 |
226 |
227 | EventDelegate getEventDelegate(){
228 | if (mEventDelegate == null)mEventDelegate = new DefaultEventDelegate(this);
229 | return mEventDelegate;
230 | }
231 |
232 | /**
233 | * @deprecated Use {@link #setMore(int, OnLoadMoreListener)} instead.
234 | */
235 | @Deprecated
236 | public void setMore(final int res, final OnLoadMoreListener listener){
237 | getEventDelegate().setMore(res, new OnMoreListener() {
238 | @Override
239 | public void onMoreShow() {
240 | listener.onLoadMore();
241 | }
242 |
243 | @Override
244 | public void onMoreClick() {
245 |
246 | }
247 | });
248 | }
249 | /**
250 | * @deprecated Use {@link #setMore(View, OnLoadMoreListener)} instead.
251 | */
252 | public void setMore(final View view,final OnLoadMoreListener listener){
253 | getEventDelegate().setMore(view, new OnMoreListener() {
254 | @Override
255 | public void onMoreShow() {
256 | listener.onLoadMore();
257 | }
258 |
259 | @Override
260 | public void onMoreClick() {
261 |
262 | }
263 | });
264 | }
265 |
266 | public void setMore(final int res, final OnMoreListener listener){
267 | getEventDelegate().setMore(res, listener);
268 | }
269 |
270 | public void setMore(final View view,OnMoreListener listener){
271 | getEventDelegate().setMore(view, listener);
272 | }
273 |
274 | public void setNoMore(final int res) {
275 | getEventDelegate().setNoMore(res,null);
276 | }
277 |
278 | public void setNoMore(final View view) {
279 | getEventDelegate().setNoMore(view,null);
280 | }
281 |
282 | public void setNoMore(final View view,OnNoMoreListener listener) {
283 | getEventDelegate().setNoMore(view,listener);
284 | }
285 |
286 | public void setNoMore(final int res,OnNoMoreListener listener) {
287 | getEventDelegate().setNoMore(res,listener);
288 | }
289 |
290 |
291 | public void setError(final int res) {
292 | getEventDelegate().setErrorMore(res,null);
293 | }
294 |
295 | public void setError(final View view) {
296 | getEventDelegate().setErrorMore(view,null);
297 | }
298 |
299 | public void setError(final int res,OnErrorListener listener) {
300 | getEventDelegate().setErrorMore(res,listener);
301 | }
302 |
303 | public void setError(final View view,OnErrorListener listener) {
304 | getEventDelegate().setErrorMore(view,listener);
305 | }
306 |
307 | @Override
308 | public void onAttachedToRecyclerView(RecyclerView recyclerView) {
309 | super.onAttachedToRecyclerView(recyclerView);
310 | this.mRecyclerView = recyclerView;
311 |
312 | //增加对RecyclerArrayAdapter奇葩操作的修复措施
313 | registerAdapterDataObserver(new FixDataObserver(mRecyclerView));
314 | }
315 |
316 | public boolean hasEventFooter(){
317 | return mEventDelegate != null;
318 | }
319 | /**
320 | * Adds the specified object at the end of the array.
321 | *
322 | * @param object The object to add at the end of the array.
323 | */
324 | public void add(T object) {
325 | if (mEventDelegate!=null)mEventDelegate.addData(object == null ? 0 : 1);
326 | if (object!=null){
327 | synchronized (mLock) {
328 | mObjects.add(object);
329 | }
330 | }
331 | if (mNotifyOnChange) notifyItemInserted(headers.size()+getCount());
332 | log("add notifyItemInserted "+(headers.size()+getCount()));
333 | }
334 | /**
335 | * Adds the specified Collection at the end of the array.
336 | *
337 | * @param collection The Collection to add at the end of the array.
338 | */
339 | public void addAll(Collection extends T> collection) {
340 | if (mEventDelegate!=null)mEventDelegate.addData(collection == null ? 0 : collection.size());
341 | if (collection!=null&&collection.size()!=0){
342 | synchronized (mLock) {
343 | mObjects.addAll(collection);
344 | }
345 | }
346 | int dataCount = collection==null?0:collection.size();
347 | if (mNotifyOnChange) notifyItemRangeInserted(headers.size()+getCount()-dataCount,dataCount);
348 | log("addAll notifyItemRangeInserted "+(headers.size()+getCount()-dataCount)+","+(dataCount));
349 |
350 | }
351 |
352 | /**
353 | * Adds the specified items at the end of the array.
354 | *
355 | * @param items The items to add at the end of the array.
356 | */
357 | public void addAll(T[] items) {
358 | if (mEventDelegate!=null)mEventDelegate.addData(items==null?0:items.length);
359 | if (items!=null&&items.length!=0) {
360 | synchronized (mLock) {
361 | Collections.addAll(mObjects, items);
362 | }
363 | }
364 | int dataCount = items==null?0:items.length;
365 | if (mNotifyOnChange) notifyItemRangeInserted(headers.size()+getCount()-dataCount,dataCount);
366 | log("addAll notifyItemRangeInserted "+((headers.size()+getCount()-dataCount)+","+(dataCount)));
367 | }
368 |
369 | /**
370 | * 插入,不会触发任何事情
371 | *
372 | * @param object The object to insert into the array.
373 | * @param index The index at which the object must be inserted.
374 | */
375 | public void insert(T object, int index) {
376 | synchronized (mLock) {
377 | mObjects.add(index, object);
378 | }
379 | if (mNotifyOnChange) notifyItemInserted(headers.size()+index);
380 | log("insert notifyItemRangeInserted "+(headers.size()+index));
381 | }
382 |
383 | /**
384 | * 插入数组,不会触发任何事情
385 | *
386 | * @param object The object to insert into the array.
387 | * @param index The index at which the object must be inserted.
388 | */
389 | public void insertAll(T[] object, int index) {
390 | synchronized (mLock) {
391 | mObjects.addAll(index, Arrays.asList(object));
392 | }
393 | int dataCount = object==null?0:object.length;
394 | if (mNotifyOnChange) notifyItemRangeInserted(headers.size()+index,dataCount);
395 | log("insertAll notifyItemRangeInserted "+((headers.size()+index)+","+(dataCount)));
396 | }
397 |
398 | /**
399 | * 插入数组,不会触发任何事情
400 | *
401 | * @param object The object to insert into the array.
402 | * @param index The index at which the object must be inserted.
403 | */
404 | public void insertAll(Collection extends T> object, int index) {
405 | synchronized (mLock) {
406 | mObjects.addAll(index, object);
407 | }
408 | int dataCount = object==null?0:object.size();
409 | if (mNotifyOnChange) notifyItemRangeInserted(headers.size()+index,dataCount);
410 | log("insertAll notifyItemRangeInserted "+((headers.size()+index)+","+(dataCount)));
411 | }
412 |
413 |
414 | public void update(T object,int pos){
415 | synchronized (mLock) {
416 | mObjects.set(pos,object);
417 | }
418 | if (mNotifyOnChange) notifyItemChanged(pos);
419 | log("insertAll notifyItemChanged "+pos);
420 | }
421 |
422 | /**
423 | * 删除,不会触发任何事情
424 | *
425 | * @param object The object to remove.
426 | */
427 | public void remove(T object) {
428 | int position = mObjects.indexOf(object);
429 | synchronized (mLock) {
430 | if (mObjects.remove(object)){
431 | if (mNotifyOnChange) notifyItemRemoved(headers.size()+position);
432 | log("remove notifyItemRemoved "+(headers.size()+position));
433 | }
434 | }
435 | }
436 |
437 | /**
438 | * 删除,不会触发任何事情
439 | *
440 | * @param position The position of the object to remove.
441 | */
442 | public void remove(int position) {
443 | synchronized (mLock) {
444 | mObjects.remove(position);
445 | }
446 | if (mNotifyOnChange) notifyItemRemoved(headers.size()+position);
447 | log("remove notifyItemRemoved "+(headers.size()+position));
448 | }
449 |
450 |
451 | /**
452 | * 触发清空
453 | * 与{@link #clear()}的不同仅在于这个使用notifyItemRangeRemoved.
454 | * 猜测这个方法与add伪并发执行的时候会造成"Scrapped or attached views may not be recycled"的Crash.
455 | * 所以建议使用{@link #clear()}
456 | */
457 | public void removeAll() {
458 | int count = mObjects.size();
459 | if (mEventDelegate!=null)mEventDelegate.clear();
460 | synchronized (mLock) {
461 | mObjects.clear();
462 | }
463 | if (mNotifyOnChange) notifyItemRangeRemoved(headers.size(),count);
464 | log("clear notifyItemRangeRemoved "+(headers.size())+","+(count));
465 | }
466 |
467 | /**
468 | * 触发清空
469 | */
470 | public void clear() {
471 | int count = mObjects.size();
472 | if (mEventDelegate!=null)mEventDelegate.clear();
473 | synchronized (mLock) {
474 | mObjects.clear();
475 | }
476 | if (mNotifyOnChange) notifyDataSetChanged();
477 | log("clear notifyItemRangeRemoved "+(headers.size())+","+(count));
478 | }
479 | /**
480 | * Sorts the content of this adapter using the specified comparator.
481 | *
482 | * @param comparator The comparator used to sort the objects contained
483 | * in this adapter.
484 | */
485 | public void sort(Comparator super T> comparator) {
486 | synchronized (mLock) {
487 | Collections.sort(mObjects, comparator);
488 | }
489 | if (mNotifyOnChange) notifyDataSetChanged();
490 | }
491 |
492 |
493 | /**
494 | * Control whether methods that change the list ({@link #add},
495 | * {@link #insert}, {@link #remove}, {@link #clear}) automatically call
496 | * {@link #notifyDataSetChanged}. If set to false, caller must
497 | * manually call notifyDataSetChanged() to have the changes
498 | * reflected in the attached view.
499 | *
500 | * The default is true, and calling notifyDataSetChanged()
501 | * resets the flag to true.
502 | *
503 | * @param notifyOnChange if true, modifications to the list will
504 | * automatically call {@link
505 | * #notifyDataSetChanged}
506 | */
507 | public void setNotifyOnChange(boolean notifyOnChange) {
508 | mNotifyOnChange = notifyOnChange;
509 | }
510 |
511 |
512 |
513 |
514 | /**
515 | * Returns the context associated with this array adapter. The context is used
516 | * to create views from the resource passed to the constructor.
517 | *
518 | * @return The Context associated with this adapter.
519 | */
520 | public Context getContext() {
521 | return mContext;
522 | }
523 |
524 | public void setContext(Context ctx) {
525 | mContext = ctx;
526 | }
527 |
528 | /**
529 | * 这个函数包含了头部和尾部view的个数,不是真正的item个数。
530 | * @return
531 | */
532 | @Deprecated
533 | @Override
534 | public final int getItemCount() {
535 | return mObjects.size()+headers.size()+footers.size();
536 | }
537 |
538 | /**
539 | * 应该使用这个获取item个数
540 | * @return
541 | */
542 | public int getCount(){
543 | return mObjects.size();
544 | }
545 |
546 | private View createSpViewByType(ViewGroup parent, int viewType){
547 | for (ItemView headerView:headers){
548 | if (headerView.hashCode() == viewType){
549 | View view = headerView.onCreateView(parent);
550 | StaggeredGridLayoutManager.LayoutParams layoutParams;
551 | if (view.getLayoutParams()!=null)
552 | layoutParams = new StaggeredGridLayoutManager.LayoutParams(view.getLayoutParams());
553 | else
554 | layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
555 | layoutParams.setFullSpan(true);
556 | view.setLayoutParams(layoutParams);
557 | return view;
558 | }
559 | }
560 | for (ItemView footerview:footers){
561 | if (footerview.hashCode() == viewType){
562 | View view = footerview.onCreateView(parent);
563 | StaggeredGridLayoutManager.LayoutParams layoutParams;
564 | if (view.getLayoutParams()!=null)
565 | layoutParams = new StaggeredGridLayoutManager.LayoutParams(view.getLayoutParams());
566 | else
567 | layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
568 | layoutParams.setFullSpan(true);
569 | view.setLayoutParams(layoutParams);
570 | return view;
571 | }
572 | }
573 | return null;
574 | }
575 |
576 | @Override
577 | public final BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
578 | View view = createSpViewByType(parent, viewType);
579 | if (view!=null){
580 | return new StateViewHolder(view);
581 | }
582 |
583 | final BaseViewHolder viewHolder = OnCreateViewHolder(parent, viewType);
584 |
585 | //itemView 的点击事件
586 | if (mItemClickListener!=null) {
587 | viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
588 | @Override
589 | public void onClick(View v) {
590 | mItemClickListener.onItemClick(viewHolder.getAdapterPosition()-headers.size());
591 | }
592 | });
593 | }
594 |
595 | if (mItemLongClickListener!=null){
596 | viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
597 | @Override
598 | public boolean onLongClick(View v) {
599 | return mItemLongClickListener.onItemLongClick(viewHolder.getAdapterPosition()-headers.size());
600 | }
601 | });
602 | }
603 | return viewHolder;
604 | }
605 |
606 | abstract public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType);
607 |
608 |
609 | @Override
610 | public final void onBindViewHolder(BaseViewHolder holder, int position) {
611 | holder.itemView.setId(position);
612 | if (headers.size()!=0 && position=0){
619 | footers.get(i).onBindView(holder.itemView);
620 | return ;
621 | }
622 | OnBindViewHolder(holder,position-headers.size());
623 | }
624 |
625 |
626 | public void OnBindViewHolder(BaseViewHolder holder, final int position){
627 | holder.setData(getItem(position));
628 | }
629 |
630 |
631 | @Deprecated
632 | @Override
633 | public final int getItemViewType(int position) {
634 | if (headers.size()!=0){
635 | if (position= 0){
651 | return footers.get(i).hashCode();
652 | }
653 | }
654 | return getViewType(position-headers.size());
655 | }
656 |
657 | public int getViewType(int position){
658 | return 0;
659 | }
660 |
661 |
662 | public List getAllData(){
663 | return new ArrayList<>(mObjects);
664 | }
665 |
666 | /**
667 | * {@inheritDoc}
668 | */
669 | public T getItem(int position) {
670 | return mObjects.get(position);
671 | }
672 |
673 | /**
674 | * Returns the position of the specified item in the array.
675 | *
676 | * @param item The item to retrieve the position of.
677 | *
678 | * @return The position of the specified item.
679 | */
680 | public int getPosition(T item) {
681 | return mObjects.indexOf(item);
682 | }
683 |
684 | /**
685 | * {@inheritDoc}
686 | */
687 | public long getItemId(int position) {
688 | return position;
689 | }
690 |
691 | private class StateViewHolder extends BaseViewHolder{
692 |
693 | public StateViewHolder(View itemView) {
694 | super(itemView);
695 | }
696 | }
697 |
698 | public interface OnItemClickListener {
699 | void onItemClick(int position);
700 | }
701 |
702 | public interface OnItemLongClickListener {
703 | boolean onItemLongClick(int position);
704 | }
705 |
706 | public void setOnItemClickListener(OnItemClickListener listener){
707 | this.mItemClickListener = listener;
708 | }
709 |
710 | public void setOnItemLongClickListener(OnItemLongClickListener listener){
711 | this.mItemLongClickListener = listener;
712 | }
713 |
714 | private static void log(String content){
715 | if (EasyRecyclerView.DEBUG){
716 | Log.i(EasyRecyclerView.TAG,content);
717 | }
718 | }
719 | }
720 |
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/java/com/jude/easyrecyclerview/decoration/DividerDecoration.java:
--------------------------------------------------------------------------------
1 | package com.jude.easyrecyclerview.decoration;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Rect;
5 | import android.graphics.drawable.ColorDrawable;
6 | import android.support.v7.widget.GridLayoutManager;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.support.v7.widget.OrientationHelper;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.support.v7.widget.StaggeredGridLayoutManager;
11 | import android.view.View;
12 |
13 | import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
14 |
15 | public class DividerDecoration extends RecyclerView.ItemDecoration{
16 | private ColorDrawable mColorDrawable;
17 | private int mHeight;
18 | private int mPaddingLeft;
19 | private int mPaddingRight;
20 | private boolean mDrawLastItem = true;
21 | private boolean mDrawHeaderFooter = false;
22 |
23 | public DividerDecoration(int color, int height) {
24 | this.mColorDrawable = new ColorDrawable(color);
25 | this.mHeight = height;
26 | }
27 | public DividerDecoration(int color, int height, int paddingLeft, int paddingRight) {
28 | this.mColorDrawable = new ColorDrawable(color);
29 | this.mHeight = height;
30 | this.mPaddingLeft = paddingLeft;
31 | this.mPaddingRight = paddingRight;
32 | }
33 |
34 | public void setDrawLastItem(boolean mDrawLastItem) {
35 | this.mDrawLastItem = mDrawLastItem;
36 | }
37 |
38 | public void setDrawHeaderFooter(boolean mDrawHeaderFooter) {
39 | this.mDrawHeaderFooter = mDrawHeaderFooter;
40 | }
41 |
42 | @Override
43 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
44 | int position = parent.getChildAdapterPosition(view);
45 | int orientation = 0;
46 | int headerCount = 0,footerCount = 0;
47 | if (parent.getAdapter() instanceof RecyclerArrayAdapter){
48 | headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
49 | footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
50 | }
51 |
52 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
53 | if (layoutManager instanceof StaggeredGridLayoutManager){
54 | orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
55 | }else if (layoutManager instanceof GridLayoutManager){
56 | orientation = ((GridLayoutManager) layoutManager).getOrientation();
57 | }else if (layoutManager instanceof LinearLayoutManager){
58 | orientation = ((LinearLayoutManager) layoutManager).getOrientation();
59 | }
60 |
61 | if (position>=headerCount&&position=dataStartPosition&&position=dataStartPosition&&position=headerCount&&position the header view holder
39 | */
40 | public interface IStickyHeaderAdapter {
41 |
42 | /**
43 | * Returns the header id for the item at the given position.
44 | * The item in one group should return the same HeaderId.
45 | *
46 | * @param position the item position
47 | * @return the header id
48 | */
49 | long getHeaderId(int position);
50 |
51 | /**
52 | * Creates a new header ViewHolder.
53 | *
54 | * @param parent the header's view parent
55 | * @return a view holder for the created view
56 | */
57 | T onCreateHeaderViewHolder(ViewGroup parent);
58 |
59 | /**
60 | * Updates the header view to reflect the header data for the given position
61 | * @param viewholder the header view holder
62 | * @param position the header's item position
63 | */
64 | void onBindHeaderViewHolder(T viewholder, int position);
65 | }
66 |
67 |
68 | public static final long NO_HEADER_ID = -1L;
69 |
70 | private Map mHeaderCache;
71 |
72 | private IStickyHeaderAdapter mAdapter;
73 |
74 | private boolean mRenderInline;
75 |
76 | private boolean mIncludeHeader = false;
77 |
78 | /**
79 | * @param adapter
80 | * the sticky header adapter to use
81 | */
82 | public StickyHeaderDecoration(IStickyHeaderAdapter adapter) {
83 | this(adapter, false);
84 | }
85 |
86 | /**
87 | * @param adapter
88 | * the sticky header adapter to use
89 | */
90 | public StickyHeaderDecoration(IStickyHeaderAdapter adapter, boolean renderInline) {
91 | mAdapter = adapter;
92 | mHeaderCache = new HashMap<>();
93 | mRenderInline = renderInline;
94 | }
95 |
96 | public void setIncludeHeader(boolean mIncludeHeader) {
97 | this.mIncludeHeader = mIncludeHeader;
98 | }
99 |
100 | /**
101 | * {@inheritDoc}
102 | */
103 | @Override
104 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
105 | int position = parent.getChildAdapterPosition(view);
106 | int headerHeight = 0;
107 |
108 | if (!mIncludeHeader){
109 | if (parent.getAdapter() instanceof RecyclerArrayAdapter){
110 | int headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
111 | int footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
112 | int dataCount = ((RecyclerArrayAdapter) parent.getAdapter()).getCount();
113 | if (position=headerCount+dataCount){
117 | return ;
118 | }
119 | if (position>=headerCount){
120 | position-=headerCount;
121 | }
122 |
123 | }
124 | }
125 |
126 | if (position != RecyclerView.NO_POSITION
127 | && hasHeader(position)
128 | && showHeaderAboveItem(position)) {
129 |
130 | View header = getHeader(parent, position).itemView;
131 | headerHeight = getHeaderHeightForLayout(header);
132 | }
133 |
134 | outRect.set(0, headerHeight, 0, 0);
135 | }
136 |
137 | private boolean showHeaderAboveItem(int itemAdapterPosition) {
138 | if (itemAdapterPosition == 0) {
139 | return true;
140 | }
141 | return mAdapter.getHeaderId(itemAdapterPosition - 1) != mAdapter.getHeaderId(itemAdapterPosition);
142 | }
143 |
144 | /**
145 | * Clears the header view cache. Headers will be recreated and
146 | * rebound on list scroll after this method has been called.
147 | */
148 | public void clearHeaderCache() {
149 | mHeaderCache.clear();
150 | }
151 |
152 | public View findHeaderViewUnder(float x, float y) {
153 | for (RecyclerView.ViewHolder holder : mHeaderCache.values()) {
154 | final View child = holder.itemView;
155 | final float translationX = ViewCompat.getTranslationX(child);
156 | final float translationY = ViewCompat.getTranslationY(child);
157 |
158 | if (x >= child.getLeft() + translationX &&
159 | x <= child.getRight() + translationX &&
160 | y >= child.getTop() + translationY &&
161 | y <= child.getBottom() + translationY) {
162 | return child;
163 | }
164 | }
165 |
166 | return null;
167 | }
168 |
169 | private boolean hasHeader(int position) {
170 | return mAdapter.getHeaderId(position) != NO_HEADER_ID;
171 | }
172 |
173 | private RecyclerView.ViewHolder getHeader(RecyclerView parent, int position) {
174 | final long key = mAdapter.getHeaderId(position);
175 |
176 | if (mHeaderCache.containsKey(key)) {
177 | return mHeaderCache.get(key);
178 | } else {
179 | final RecyclerView.ViewHolder holder = mAdapter.onCreateHeaderViewHolder(parent);
180 | final View header = holder.itemView;
181 |
182 | //noinspection unchecked
183 | mAdapter.onBindHeaderViewHolder(holder, position);
184 |
185 | int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.EXACTLY);
186 | int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.UNSPECIFIED);
187 |
188 | int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
189 | parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width);
190 | int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
191 | parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height);
192 |
193 | header.measure(childWidth, childHeight);
194 | header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
195 |
196 | mHeaderCache.put(key, holder);
197 |
198 | return holder;
199 | }
200 | }
201 |
202 | /**
203 | * {@inheritDoc}
204 | */
205 | @Override
206 | public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
207 | if (parent.getAdapter() == null){
208 | return;
209 | }
210 |
211 | final int count = parent.getChildCount();
212 | long previousHeaderId = -1;
213 |
214 | for (int layoutPos = 0; layoutPos < count; layoutPos++) {
215 | final View child = parent.getChildAt(layoutPos);
216 | int adapterPos = parent.getChildAdapterPosition(child);
217 |
218 | if (!mIncludeHeader){
219 | if (parent.getAdapter() instanceof RecyclerArrayAdapter){
220 | int headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
221 | int footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
222 | int dataCount = ((RecyclerArrayAdapter) parent.getAdapter()).getCount();
223 | if (adapterPos=headerCount+dataCount){
227 | continue ;
228 | }
229 | if (adapterPos>=headerCount){
230 | adapterPos-=headerCount;
231 | }
232 |
233 | }
234 | }
235 |
236 | if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
237 | long headerId = mAdapter.getHeaderId(adapterPos);
238 |
239 | if (headerId != previousHeaderId) {
240 | previousHeaderId = headerId;
241 | View header = getHeader(parent, adapterPos).itemView;
242 | canvas.save();
243 |
244 | final int left = child.getLeft();
245 | final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
246 | canvas.translate(left, top);
247 |
248 | header.setTranslationX(left);
249 | header.setTranslationY(top);
250 | header.draw(canvas);
251 | canvas.restore();
252 | }
253 | }
254 | }
255 | }
256 |
257 | private int getHeaderTop(RecyclerView parent, View child, View header, int adapterPos, int layoutPos) {
258 | int headerHeight = getHeaderHeightForLayout(header);
259 | int top = ((int) child.getY()) - headerHeight;
260 | if (layoutPos == 0) {
261 | final int count = parent.getChildCount();
262 | final long currentId = mAdapter.getHeaderId(adapterPos);
263 | // find next view with header and compute the offscreen push if needed
264 | for (int i = 1; i < count; i++) {
265 | int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(i));
266 | if (adapterPosHere != RecyclerView.NO_POSITION) {
267 | long nextId = mAdapter.getHeaderId(adapterPosHere);
268 | if (nextId != currentId) {
269 | final View next = parent.getChildAt(i);
270 | final int offset = ((int) next.getY()) - (headerHeight + getHeader(parent, adapterPosHere).itemView.getHeight());
271 | if (offset < 0) {
272 | return offset;
273 | } else {
274 | break;
275 | }
276 | }
277 | }
278 | }
279 |
280 | top = Math.max(0, top);
281 | }
282 |
283 | return top;
284 | }
285 |
286 | private int getHeaderHeightForLayout(View header) {
287 | return mRenderInline ? 0 : header.getHeight();
288 | }
289 | }
290 |
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/java/com/jude/easyrecyclerview/swipe/CircleImageView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jude.easyrecyclerview.swipe;
18 |
19 | import android.content.Context;
20 | import android.graphics.Canvas;
21 | import android.graphics.Color;
22 | import android.graphics.Paint;
23 | import android.graphics.RadialGradient;
24 | import android.graphics.Shader;
25 | import android.graphics.drawable.ShapeDrawable;
26 | import android.graphics.drawable.shapes.OvalShape;
27 | import android.support.v4.content.ContextCompat;
28 | import android.support.v4.view.ViewCompat;
29 | import android.view.animation.Animation;
30 | import android.widget.ImageView;
31 |
32 | /**
33 | * Private class created to work around issues with AnimationListeners being
34 | * called before the animation is actually complete and support shadows on older
35 | * platforms.
36 | */
37 | class CircleImageView extends ImageView {
38 |
39 | private static final int KEY_SHADOW_COLOR = 0x1E000000;
40 | private static final int FILL_SHADOW_COLOR = 0x3D000000;
41 | // PX
42 | private static final float X_OFFSET = 0f;
43 | private static final float Y_OFFSET = 1.75f;
44 | private static final float SHADOW_RADIUS = 3.5f;
45 | private static final int SHADOW_ELEVATION = 4;
46 |
47 | private Animation.AnimationListener mListener;
48 | int mShadowRadius;
49 |
50 | CircleImageView(Context context, int color) {
51 | super(context);
52 | final float density = getContext().getResources().getDisplayMetrics().density;
53 | final int shadowYOffset = (int) (density * Y_OFFSET);
54 | final int shadowXOffset = (int) (density * X_OFFSET);
55 |
56 | mShadowRadius = (int) (density * SHADOW_RADIUS);
57 |
58 | ShapeDrawable circle;
59 | if (elevationSupported()) {
60 | circle = new ShapeDrawable(new OvalShape());
61 | ViewCompat.setElevation(this, SHADOW_ELEVATION * density);
62 | } else {
63 | OvalShape oval = new OvalShadow(mShadowRadius);
64 | circle = new ShapeDrawable(oval);
65 | ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, circle.getPaint());
66 | circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset,
67 | KEY_SHADOW_COLOR);
68 | final int padding = mShadowRadius;
69 | // set padding so the inner image sits correctly within the shadow.
70 | setPadding(padding, padding, padding, padding);
71 | }
72 | circle.getPaint().setColor(color);
73 | setBackgroundDrawable(circle);
74 | }
75 |
76 | private boolean elevationSupported() {
77 | return android.os.Build.VERSION.SDK_INT >= 21;
78 | }
79 |
80 | @Override
81 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
82 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
83 | if (!elevationSupported()) {
84 | setMeasuredDimension(getMeasuredWidth() + mShadowRadius * 2, getMeasuredHeight()
85 | + mShadowRadius * 2);
86 | }
87 | }
88 |
89 | public void setAnimationListener(Animation.AnimationListener listener) {
90 | mListener = listener;
91 | }
92 |
93 | @Override
94 | public void onAnimationStart() {
95 | super.onAnimationStart();
96 | if (mListener != null) {
97 | mListener.onAnimationStart(getAnimation());
98 | }
99 | }
100 |
101 | @Override
102 | public void onAnimationEnd() {
103 | super.onAnimationEnd();
104 | if (mListener != null) {
105 | mListener.onAnimationEnd(getAnimation());
106 | }
107 | }
108 |
109 | /**
110 | * Update the background color of the circle image view.
111 | *
112 | * @param colorRes Id of a color resource.
113 | */
114 | public void setBackgroundColorRes(int colorRes) {
115 | setBackgroundColor(ContextCompat.getColor(getContext(), colorRes));
116 | }
117 |
118 | @Override
119 | public void setBackgroundColor(int color) {
120 | if (getBackground() instanceof ShapeDrawable) {
121 | ((ShapeDrawable) getBackground()).getPaint().setColor(color);
122 | }
123 | }
124 |
125 | private class OvalShadow extends OvalShape {
126 | private RadialGradient mRadialGradient;
127 | private Paint mShadowPaint;
128 |
129 | OvalShadow(int shadowRadius) {
130 | super();
131 | mShadowPaint = new Paint();
132 | mShadowRadius = shadowRadius;
133 | updateRadialGradient((int) rect().width());
134 | }
135 |
136 | @Override
137 | protected void onResize(float width, float height) {
138 | super.onResize(width, height);
139 | updateRadialGradient((int) width);
140 | }
141 |
142 | @Override
143 | public void draw(Canvas canvas, Paint paint) {
144 | final int viewWidth = CircleImageView.this.getWidth();
145 | final int viewHeight = CircleImageView.this.getHeight();
146 | canvas.drawCircle(viewWidth / 2, viewHeight / 2, viewWidth / 2, mShadowPaint);
147 | canvas.drawCircle(viewWidth / 2, viewHeight / 2, viewWidth / 2 - mShadowRadius, paint);
148 | }
149 |
150 | private void updateRadialGradient(int diameter) {
151 | mRadialGradient = new RadialGradient(diameter / 2, diameter / 2,
152 | mShadowRadius, new int[] { FILL_SHADOW_COLOR, Color.TRANSPARENT },
153 | null, Shader.TileMode.CLAMP);
154 | mShadowPaint.setShader(mRadialGradient);
155 | }
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/res/layout/layout_progress_recyclerview.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
12 |
18 |
19 |
25 |
26 |
32 |
33 |
--------------------------------------------------------------------------------
/easyrecyclerview/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Sun Nov 15 16:29:26 CST 2015
16 |
17 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jude95/EasyRecyclerView/173ef7c5d053c8d82ca2f805cacccc3e2b6827d8/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Aug 07 20:39:17 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/recycler3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jude95/EasyRecyclerView/173ef7c5d053c8d82ca2f805cacccc3e2b6827d8/recycler3.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':easyrecyclerview', ':demo'
2 |
--------------------------------------------------------------------------------