-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbsRecyclerViewAdapter.java
More file actions
509 lines (441 loc) · 16.5 KB
/
Copy pathAbsRecyclerViewAdapter.java
File metadata and controls
509 lines (441 loc) · 16.5 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package com.realmo.utils;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.com.zlct.egoupublic.util.Constant;
/**
* RecyclerView的多(单)布局 适配器
* 可以实现ListView、GridView和瀑布流的效果
*
* 配置数据 -- 必须重写{@link #onBindHolder(RecyclerViewHolder, Object, int)}
* 添加头部 -- {@link #addHeaderView(View)}
* -- 添加头部后,部分item刷新(如{@link #notifyItemChanged(int)})时
* -- 不可直接使用原始数据源的下标
* -- 因父类方法为final,不可重写,添加头部时可调用自定义方法{@link #notifyItemChange(int)}
* 多布局实现 -- 必须重写{@link #getItemType(T)}方法
* -- 强制要求数据源中有一个type名称的属性,重写时返回该值
* -- type非0时 当前item默认宽度撑满
* -- 如需不撑满 需重写{@link #isItemFullSpan(int)} 根据type的值返回是否撑满
*/
public abstract class AbsRecyclerViewAdapter<T> extends RecyclerView.Adapter<AbsRecyclerViewAdapter.RecyclerViewHolder>{
protected Context context;
protected List<T> data;
private int[] resId;//多布局的布局文件
public static final int TYPE_HEADER = -2;//第一条头部的类型
public static final int TYPE_EMPTY = -1;//空数据的类型
private List<View> mHeaderViews = new ArrayList<>();
private View mEmpty;
private OnItemClickListener onItemClickListener;
private OnItemLongClickListener onItemLongClickListener;
public AbsRecyclerViewAdapter(Context context, int... resId) {
this.context = context;
this.resId = resId;
data = new ArrayList<>();
}
public AbsRecyclerViewAdapter(Context context, List<T> data, int... resId) {
this.context = context;
this.resId = resId;
this.data = data;
}
public void setData(List<T> data){
this.data = data;
setEmptyVisible();
this.notifyDataSetChanged();
}
public void addData(List<T> data){
this.data.addAll(data);
this.notifyDataSetChanged();
}
public List<T> getData(){
return data;
}
public void deleteItem(int position){
this.data.remove(position);
this.notifyDataSetChanged();
}
/**
* Item点击监听 -- 添加头部后,头部的点击事件需自行处理
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View v, int position);
}
/**
* Item长按监听 -- 添加头部后,头部的长按事件需自行处理
*/
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
this.onItemLongClickListener = onItemLongClickListener;
}
public interface OnItemLongClickListener {
boolean onItemLongClick(View v, int position);
}
@Override
public int getItemCount() {
if (data == null) {
return getHeaderCount() + getEmptyCount();
}
return data.size() + getHeaderCount() + getEmptyCount();
}
public int getEmptyCount() {
if (data == null) {
return mEmpty != null && getHeaderCount() == 0 ? 1 : 0;
}
return mEmpty != null && data.size() + getHeaderCount() == 0 ? 1 : 0;
}
public int getHeaderCount() {
return mHeaderViews.size();
}
public void addHeaderView(View header) {
if (header == null) {
throw new RuntimeException("header is null");
}
mHeaderViews.add(header);
this.notifyDataSetChanged();
}
public void clearHeader() {
if (getHeaderCount() > 0) {
mHeaderViews.clear();
this.notifyDataSetChanged();
}
}
public void setEmptyView(View empty) {
if (empty == null) {
throw new RuntimeException("empty is null");
}
mEmpty = empty;
setEmptyLayoutParams();
}
public void setEmptyLayoutParams() {
mEmpty.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.MATCH_PARENT));
}
public void setEmptyView(int resId) {
setEmptyView(LayoutInflater.from(context).inflate(resId, null));
}
public void setEmptyVisible() {
if (mEmpty == null) {
return;
}
if (mEmpty.getVisibility() != View.VISIBLE) {
mEmpty.setVisibility(View.VISIBLE);
}
}
public View getEmptyView() {
return mEmpty;
}
/**
* 修改空数据提示中的文字
*/
public void setEmptyTips(int id, String tips) {
if (mEmpty != null) {
View v = mEmpty.findViewById(id);
if (v instanceof TextView) {
((TextView) v).setText(tips);
}
}
}
public void notifyItemChange(int position) {
super.notifyItemChanged(position + getHeaderCount());
}
public void notifyItemRangeChange(int positionStart, int itemCount) {
super.notifyItemRangeChanged(positionStart + getHeaderCount(), itemCount);
}
public void notifyItemInsert(int position) {
super.notifyItemInserted(position + getHeaderCount());
}
public void notifyItemMove(int fromPosition, int toPosition) {
super.notifyItemMoved(fromPosition + getHeaderCount(), toPosition + getHeaderCount());
}
public void notifyItemRangeInsert(int positionStart, int itemCount) {
super.notifyItemRangeInserted(positionStart + getHeaderCount(), itemCount);
}
public void notifyItemRemove(int position) {
super.notifyItemRemoved(position + getHeaderCount());
}
public void notifyItemRangeRemove(int positionStart, int itemCount) {
super.notifyItemRangeRemoved(positionStart + getHeaderCount(), itemCount);
}
/**
* 因支持添加头部功能 该方法不能重写 也不能直接在外部调用
* 使用多布局时 只能重写 {@link #getItemType(T)}
*/
@Override
public int getItemViewType(int position) {
int count = getHeaderCount();
if (count > 0 && position < count) {//头部的类型
return TYPE_HEADER - position;
}
if (getEmptyCount() > 0) {
return TYPE_EMPTY;
}
return getItemType(data.get(position - count));
}
/**
* 当前position所对应的Item的类型
* 默认Item的类型为0,其他Item的类型依次递增
*/
public int getItemType(T d) {
return super.getItemViewType(0);
}
/**
* 当前position所对应的Item 是否宽度撑满 -- 默认非0撑满 (此方法不能重写)
*/
private boolean isFullSpan(int position) {
int type = getItemViewType(position);
if (type == 0) {//默认Item
return false;
} else if (type < 0) {//头部
return true;
} else {//其他类型
return isItemFullSpan(type);
}
}
/**
* 多布局时 类型为type的item 是否宽度撑满 -- 默认非0撑满
*/
protected boolean isItemFullSpan(int type) {
return true;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType <= TYPE_HEADER) {
return new RecyclerViewHolder(mHeaderViews.get(TYPE_HEADER - viewType));
}
if (viewType == TYPE_EMPTY) {
return new RecyclerViewHolder(mEmpty);
}
View view = LayoutInflater.from(context).inflate(resId[viewType], parent, false);
return new RecyclerViewHolder(view);
}
/**
* 因支持添加头部功能 该方法不能重写 只能重写 {@link #onBindHolder(RecyclerViewHolder, Object, int)}
*/
@Override
public void onBindViewHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, int position) {
if (getItemViewType(position) <= TYPE_EMPTY) {
return;
}
onBindHolder(holder, data.get(position - getHeaderCount()), position - getHeaderCount());
}
/**
* 配置数据 -- holder的类型必须是 AbsRecyclerViewAdapter.RecyclerViewHolder
* 如需给整条Item setTag(),请调用 {@link RecyclerViewHolder#setItemTag(int, Object)}
*/
public abstract void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, T d, int position);
/**
* GridLayoutManager时 设置type不为0的item 占整行位置
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if(manager instanceof GridLayoutManager) {
final GridLayoutManager gridManager = ((GridLayoutManager) manager);
gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return isFullSpan(position) ? gridManager.getSpanCount() : 1;
}
});
}
}
/**
* StaggeredGridLayoutManager时 设置type不为0的item 占整行位置
*/
@Override
public void onViewAttachedToWindow(AbsRecyclerViewAdapter.RecyclerViewHolder holder) {
super.onViewAttachedToWindow(holder);
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
if(lp != null) {
if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) lp;
layoutParams.setFullSpan(holder.getParent().isFullSpan(holder.getLayoutPosition()));
} else {
/**
* LinearLayoutManager时 头部item撑满
*/
lp.width = RecyclerView.LayoutParams.MATCH_PARENT;
holder.itemView.setLayoutParams(lp);
}
}
}
protected class RecyclerViewHolder extends RecyclerView.ViewHolder {
Map<Integer, View> cacheMap = new HashMap();
View layoutView;
public RecyclerViewHolder(View itemView) {
super(itemView);
if(onItemClickListener != null) {
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = RecyclerViewHolder.this.getLayoutPosition();
if (position >= getHeaderCount() + getEmptyCount()) {
onItemClickListener.onItemClick(v, position - getHeaderCount());
}
}
});
}
if (onItemLongClickListener != null) {
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int position = RecyclerViewHolder.this.getLayoutPosition();
if (position >= getHeaderCount() + getEmptyCount()) {
return onItemLongClickListener.onItemLongClick(v, position - getHeaderCount());
}
return true;
}
});
}
this.layoutView = itemView;
}
private AbsRecyclerViewAdapter getParent() {
return AbsRecyclerViewAdapter.this;
}
public View getView(int id){
if(cacheMap.containsKey(id)){
return cacheMap.get(id);
} else {
View view = layoutView.findViewById(id);
cacheMap.put(id, view);
return view;
}
}
public RecyclerViewHolder setViewLayoutParams(int width, int height){
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) layoutView.getLayoutParams();
layoutParams.width = width;
layoutParams.height = height;
layoutView.setLayoutParams(layoutParams);
return this;
}
/**
* 给整条item设置tag
*/
public RecyclerViewHolder setItemTag(int id, Object tag) {
layoutView.setTag(id, tag);
return this;
}
public RecyclerViewHolder setViewVisible(int id, int visible) {
getView(id).setVisibility(visible);
return this;
}
public RecyclerViewHolder setViewSelected(int id, boolean selected) {
getView(id).setSelected(selected);
return this;
}
public RecyclerViewHolder setClickListener(int id, View.OnClickListener listener) {
getView(id).setOnClickListener(listener);
return this;
}
public RecyclerViewHolder setClickListenerAndTag(int id, View.OnClickListener listener, Object tag) {
View v = getView(id);
v.setTag(tag);
v.setOnClickListener(listener);
return this;
}
/**
* 绑定TextView
*/
public RecyclerViewHolder bindTextView(int id, String text){
TextView tv = (TextView) getView(id);
tv.setText(text);
return this;
}
/**
* 绑定TextView
*/
public RecyclerViewHolder bindTextViewWithClickListener(int id, String text, View.OnClickListener listener){
TextView tv = (TextView) getView(id);
tv.setText(text);
tv.setOnClickListener(listener);
return this;
}
/**
* 绑定TextView
*/
public RecyclerViewHolder bindTextViewWithSelected(int id, String text, boolean isSelect){
TextView tv = (TextView) getView(id);
tv.setText(text);
tv.setSelected(isSelect);
return this;
}
/**
* 绑定TextView 含html标签
*/
public RecyclerViewHolder bindTextViewWithHtml(int id, String text){
TextView tv = (TextView) getView(id);
tv.setText(Html.fromHtml(text));
return this;
}
/**
* 绑定ImageView
*/
public RecyclerViewHolder bindImageView(int id, int imgId){
ImageView iv = (ImageView) getView(id);
iv.setImageResource(imgId);
return this;
}
/**
* 绑定SimpleDraweeView
*/
public RecyclerViewHolder bindSimpleDraweeView(int id, String url){
bindSimpleDraweeView(id, Uri.parse(url));
return this;
}
/**
* 绑定SimpleDraweeView
*/
public RecyclerViewHolder bindSimpleDraweeView(int id, Uri uri){
SimpleDraweeView sdv = (SimpleDraweeView) getView(id);
sdv.setImageURI(uri);
return this;
}
/**
* 绑定SimpleDraweeView
*/
public RecyclerViewHolder bindSimpleDraweeView(int id, int imgId){
SimpleDraweeView sdv = (SimpleDraweeView) getView(id);
sdv.setImageURI(Uri.parse("res:///" + imgId));
return this;
}
/**
* 绑定SimpleDraweeView
*/
public RecyclerViewHolder bindSimpleDraweeViewBase(int id, String url){
return bindSimpleDraweeView(id, Constant.URL.BaseImg + url);
}
/**
* 设置进度条进度
*/
public RecyclerViewHolder bindProgressBar(int id, int num){
ProgressBar progressBar = (ProgressBar) getView(id);
progressBar.setProgress(num);
return this;
}
/**
* 设置星星进度条
*/
public RecyclerViewHolder bindRatingBar(int id, float rating){
RatingBar ratingBar = (RatingBar) getView(id);
ratingBar.setRating(rating);
return this;
}
}
}