/
StickyHeaderViewAdapter.java
135 lines (109 loc) · 3.94 KB
/
StickyHeaderViewAdapter.java
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
package tellh.com.stickyheaderview_rv.adapter;
import android.support.annotation.LayoutRes;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class StickyHeaderViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static int SIZE_VIEW_BINDER_POOL = 10;
private SparseArrayCompat<ViewBinder> viewBinderPool;
protected List<DataBean> displayList;
private DataSetChangeListener onDataSetChangeListener;
public StickyHeaderViewAdapter(List<? extends DataBean> displayList) {
viewBinderPool = new SparseArrayCompat<>(SIZE_VIEW_BINDER_POOL);
// if (displayList == null)
this.displayList = new ArrayList<>();
this.displayList.addAll(displayList);
}
private List<DataBean> getDisplayList() {
return displayList;
}
public int getDisplayListSize() {
return displayList == null ? 0 : displayList.size();
}
public DataBean get(int i) {
if (i < getDisplayListSize())
return displayList.get(i);
else return null;
}
public int getPosition(DataBean dataBean) {
return displayList.indexOf(dataBean);
}
@Override
public int getItemViewType(int position) {
return displayList.get(position).getItemLayoutId(this);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewBinder viewBinder = getViewBinder(viewType);
View itemView = LayoutInflater.from(parent.getContext())
.inflate(viewBinder.getItemLayoutId(this), parent, false);
return viewBinder.provideViewHolder(itemView);
}
public ViewBinder getViewBinder(int viewType) {
return viewBinderPool.get(viewType);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//normal view
DataBean dataBean = displayList.get(position);
dataBean.provideViewBinder(this, viewBinderPool, position).bindView(this, holder, position, dataBean);
}
@Override
public int getItemCount() {
return displayList == null ? 0 : displayList.size();
}
public ViewBinder findViewBinderFromPool(@LayoutRes int layoutId) {
return viewBinderPool.get(layoutId);
}
public static void setViewBinderPoolInitSize(int size) {
SIZE_VIEW_BINDER_POOL = size;
}
public void clearViewBinderCache() {
viewBinderPool.clear();
}
public void append(List<? extends DataBean> list) {
if (list == null)
return;
displayList.addAll(list);
notifyDataSetChanged();
}
public void append(DataBean dataBean) {
displayList.add(dataBean);
notifyItemInserted(displayList.size() - 1);
}
public void refresh(List<? extends DataBean> list) {
if (list == null)
return;
onDataSetChangeListener.onClearAll();
displayList.clear();
displayList.addAll(list);
notifyDataSetChanged();
}
public void delete(int pos) {
onDataSetChangeListener.remove(pos);
displayList.remove(pos);
notifyItemRemoved(pos);
}
public void clear(RecyclerView recyclerView) {
onDataSetChangeListener.onClearAll();
displayList.clear();
notifyDataSetChanged();
recyclerView.scrollToPosition(0);
}
public StickyHeaderViewAdapter RegisterItemType(ViewBinder viewBinder) {
viewBinderPool.put(viewBinder.getItemLayoutId(this), viewBinder);
return this;
}
// Don't Call this Method
public void setDataSetChangeListener(DataSetChangeListener listener) {
this.onDataSetChangeListener = listener;
}
public interface DataSetChangeListener {
void onClearAll();
void remove(int pos);
}
}