-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHKBaseRecyclerAdapter.java
More file actions
70 lines (55 loc) · 2.28 KB
/
HKBaseRecyclerAdapter.java
File metadata and controls
70 lines (55 loc) · 2.28 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
public abstract class HKBaseRecyclerAdapter<T> extends RecyclerView.Adapter<HKViewHolder> implements HKViewHolder.HKViewHolderClickListener {
protected abstract @LayoutRes int onCreateLayout();
protected abstract @IdRes int[] setChildsToInflate();
protected abstract @IdRes int[] setClickableChilds();
protected abstract void onBind(T item, HKViewHolder viewHolder);
private List<T> items = new ArrayList<>();
private OnRowClickListener onRowClickListener;
private HKViewHolder.Clickables clickables = HKViewHolder.Clickables.NONE;
public interface OnRowClickListener {
void onRowClicked(View view, int position, Object item, boolean isChildView);
}
public HKBaseRecyclerAdapter() {
items = new ArrayList<>();
}
@Override
public HKViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(onCreateLayout(), viewGroup, false);
HKViewHolder.HKViewHolderClickListener viewHolderClickListener = this;
return HKViewHolder.constructWithParameters(view, viewHolderClickListener, setChildsToInflate(), setClickableChilds(), clickables);
}
@Override
public void onBindViewHolder(HKViewHolder holder, int position) {
onBind(getItem(position), holder);
}
@Override
public void onViewHolderClick(View view, int position, boolean isChildView) {
if (onRowClickListener != null) {
onRowClickListener.onRowClicked(view, position, getItem(position), isChildView);
}
}
@Override
public int getItemCount() {
return items.size();
}
public T getItem(int index) {
return (index < items.size() ? items.get(index) : null);
}
public void setItems(List<T> items) {
setItems(items,false);
}
public void setItems(List<T> items,boolean autoRefresh) {
if (items != null) {
this.items = items;
if (autoRefresh){
notifyDataSetChanged();
}
}
}
public void setOnRowClickListener(OnRowClickListener onRowClickListener) {
this.onRowClickListener = onRowClickListener;
}
public void setClickableType(HKViewHolder.Clickables clickables) {
this.clickables = clickables;
}
}