Skip to content

Commit b90abfa

Browse files
committed
add ArrayRecyclerAdapter and refactor CommonRecyclerAdapter
1 parent 5b516f8 commit b90abfa

2 files changed

Lines changed: 167 additions & 119 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package com.github.zeng1990java.commonadapter;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.v7.widget.RecyclerView;
5+
6+
import java.util.ArrayList;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
10+
/**
11+
* $desc
12+
*
13+
* @author zxb
14+
* @date 16/8/6 下午5:10
15+
*/
16+
public abstract class ArrayRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
17+
18+
protected List<T> mDatas;
19+
20+
public ArrayRecyclerAdapter(){
21+
this(new ArrayList<T>());
22+
}
23+
24+
public ArrayRecyclerAdapter(@NonNull List<T> datas){
25+
mDatas = datas;
26+
}
27+
28+
@Override
29+
public int getItemCount() {
30+
return mDatas.size();
31+
}
32+
33+
public T getItem(int position){
34+
return mDatas.get(position);
35+
}
36+
37+
public List<T> getData(){
38+
return mDatas;
39+
}
40+
41+
public void add(T data){
42+
add(0, data);
43+
}
44+
45+
public void add(int position, T data){
46+
mDatas.add(position, data);
47+
notifyItemInserted(position+getAdapterPosition(0));
48+
}
49+
50+
public void remove(int position){
51+
remove(position, null);
52+
}
53+
54+
public void remove(int position, T data){
55+
mDatas.remove(position);
56+
notifyItemRemoved(position+getAdapterPosition(0));
57+
}
58+
59+
public void addAll(@NonNull List<T> datas){
60+
int positionStart = mDatas.size();
61+
mDatas.addAll(datas);
62+
notifyItemRangeInserted(positionStart + getAdapterPosition(0), datas.size());
63+
}
64+
65+
public void replaceAll(@NonNull List<T> datas){
66+
if (mDatas.equals(datas)){
67+
return;
68+
}
69+
70+
if (mDatas.isEmpty() && datas.isEmpty()) {
71+
return;
72+
}
73+
74+
if (mDatas.isEmpty()){
75+
addAll(datas);
76+
return;
77+
}
78+
79+
if (datas.isEmpty()){
80+
clear();
81+
return;
82+
}
83+
84+
// 首先将就列表有,新列表没有的从旧列表中移除
85+
retainAll(datas);
86+
87+
// 如果列表空了,直接插入全部就好了
88+
if (mDatas.isEmpty()) {
89+
addAll(datas);
90+
return;
91+
}
92+
93+
// 遍历新列表,对旧列表数据进行更新,增加,删除
94+
for (int indexNew = 0; indexNew < datas.size(); indexNew++) {
95+
96+
T item = datas.get(indexNew);
97+
int indexOld = mDatas.indexOf(item);
98+
if (indexOld == -1){
99+
add(indexNew, item);
100+
}else if (indexNew == indexOld){
101+
set(indexNew, item);
102+
}else {
103+
mDatas.remove(indexOld);
104+
mDatas.add(indexNew, item);
105+
notifyItemMoved(indexOld + getAdapterPosition(0), indexNew + getAdapterPosition(0));
106+
}
107+
}
108+
109+
}
110+
111+
public void set(int postion, T data){
112+
mDatas.set(postion, data);
113+
notifyItemChanged(getAdapterPosition(0) + postion);
114+
}
115+
116+
public void set(T oldData, T newData){
117+
if (contains(oldData)) {
118+
set(mDatas.indexOf(oldData), newData);
119+
}
120+
}
121+
122+
public void retainAll(@NonNull List<T> datas){
123+
if (datas.isEmpty()){
124+
clear();
125+
return;
126+
}
127+
Iterator<T> iterator = mDatas.iterator();
128+
while (iterator.hasNext()){
129+
T next = iterator.next();
130+
if (datas.contains(next)){
131+
continue;
132+
}
133+
int index = mDatas.indexOf(next);
134+
iterator.remove();
135+
notifyItemRemoved(getAdapterPosition(0) + index);
136+
}
137+
138+
}
139+
140+
public void clear(){
141+
if (mDatas.isEmpty()){
142+
return;
143+
}
144+
int size = mDatas.size();
145+
mDatas.clear();
146+
notifyItemRangeRemoved(getAdapterPosition(0), size);
147+
}
148+
149+
public boolean contains(T data){
150+
return mDatas.contains(data);
151+
}
152+
153+
/**
154+
* real data position
155+
* @param dataPosition
156+
* @return
157+
*/
158+
protected int getAdapterPosition(int dataPosition){
159+
return dataPosition;
160+
}
161+
}

commonadapter/src/main/java/com/github/zeng1990java/commonadapter/CommonRecyclerAdapter.java

Lines changed: 6 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import android.widget.LinearLayout;
1313

1414
import java.util.ArrayList;
15-
import java.util.Iterator;
1615
import java.util.List;
1716

1817
/**
@@ -21,7 +20,7 @@
2120
* @author zxb
2221
* @date 16/7/16 下午11:54
2322
*/
24-
public abstract class CommonRecyclerAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
23+
public abstract class CommonRecyclerAdapter<T> extends ArrayRecyclerAdapter<T,ViewHolder> {
2524

2625
public interface OnItemClickListener{
2726
void onItemClick(ViewHolder holder, int position);
@@ -37,7 +36,6 @@ public interface OnLoadMoreListener{
3736

3837
private Context mContext;
3938
private int mLayoutId;
40-
private List<T> mDatas;
4139
private LayoutInflater mInflater;
4240

4341
private LinearLayout mHeaderLayout;
@@ -292,122 +290,6 @@ public int getItemLayoutResId(T data, int position){
292290
return mLayoutId;
293291
}
294292

295-
public T getItem(int position){
296-
return mDatas.get(position);
297-
}
298-
299-
public List<T> getData(){
300-
return mDatas;
301-
}
302-
303-
public void add(T data){
304-
add(0, data);
305-
}
306-
307-
public void add(int position, T data){
308-
mDatas.add(position, data);
309-
notifyItemInserted(position+getHeaderViewItemCount());
310-
}
311-
312-
public void remove(int position, T data){
313-
mDatas.remove(position);
314-
notifyItemRemoved(position+getHeaderViewItemCount());
315-
}
316-
317-
public void addAll(@NonNull List<T> datas){
318-
int positionStart = mDatas.size();
319-
mDatas.addAll(datas);
320-
notifyItemRangeInserted(positionStart + getHeaderViewItemCount(), datas.size());
321-
}
322-
323-
public void replaceAll(@NonNull List<T> datas){
324-
if (mDatas.equals(datas)){
325-
return;
326-
}
327-
328-
if (mDatas.isEmpty() && datas.isEmpty()) {
329-
return;
330-
}
331-
332-
if (mDatas.isEmpty()){
333-
addAll(datas);
334-
return;
335-
}
336-
337-
if (datas.isEmpty()){
338-
clear();
339-
return;
340-
}
341-
342-
// 首先将就列表有,新列表没有的从旧列表中移除
343-
retainAll(datas);
344-
345-
// 如果列表空了,直接插入全部就好了
346-
if (mDatas.isEmpty()) {
347-
addAll(datas);
348-
return;
349-
}
350-
351-
// 遍历新列表,对旧列表数据进行更新,增加,删除
352-
for (int indexNew = 0; indexNew < datas.size(); indexNew++) {
353-
354-
T item = datas.get(indexNew);
355-
int indexOld = mDatas.indexOf(item);
356-
if (indexOld == -1){
357-
add(indexNew, item);
358-
}else if (indexNew == indexOld){
359-
set(indexNew, item);
360-
}else {
361-
mDatas.remove(indexOld);
362-
mDatas.add(indexNew, item);
363-
notifyItemMoved(indexOld + getHeaderViewItemCount(), indexNew + getHeaderViewItemCount());
364-
}
365-
}
366-
367-
}
368-
369-
public void set(int postion, T data){
370-
mDatas.set(postion, data);
371-
notifyItemChanged(getHeaderViewItemCount() + postion);
372-
}
373-
374-
public void set(T oldData, T newData){
375-
if (contains(oldData)) {
376-
set(mDatas.indexOf(oldData), newData);
377-
}
378-
}
379-
380-
public void retainAll(@NonNull List<T> datas){
381-
if (datas.isEmpty()){
382-
clear();
383-
return;
384-
}
385-
Iterator<T> iterator = mDatas.iterator();
386-
while (iterator.hasNext()){
387-
T next = iterator.next();
388-
if (datas.contains(next)){
389-
continue;
390-
}
391-
int index = mDatas.indexOf(next);
392-
iterator.remove();
393-
notifyItemRemoved(getHeaderViewItemCount() + index);
394-
}
395-
396-
}
397-
398-
public void clear(){
399-
if (mDatas.isEmpty()){
400-
return;
401-
}
402-
int size = mDatas.size();
403-
mDatas.clear();
404-
notifyItemRangeRemoved(getHeaderViewItemCount(), size);
405-
}
406-
407-
public boolean contains(T data){
408-
return mDatas.contains(data);
409-
}
410-
411293
@Override
412294
public int getItemCount() {
413295
return getDataItemCount() + getHeaderViewItemCount() + getFooterViewItemCount() + getLoadMoreViewItemCount();
@@ -461,4 +343,9 @@ private boolean hasFooterView(){
461343
private boolean hasLoadMoreView(){
462344
return isHasLoadMore;
463345
}
346+
347+
@Override
348+
protected int getAdapterPosition(int dataPosition) {
349+
return getHeaderViewItemCount() + dataPosition;
350+
}
464351
}

0 commit comments

Comments
 (0)