BaseBindingAdapter.java
4.52 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
package com.cnlive.goldenline.baseadpter;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.cnlive.goldenline.BR;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ZJL on 2017/5/14.
*/
public class BaseBindingAdapter<D, B extends ViewDataBinding> extends RecyclerView.Adapter<BaseBindingVH<B>> {
protected Context mContext;
protected int mLayoutId;
protected List<D> mDatas;
protected LayoutInflater mInfalter;
//用于设置Item的事件Presenter
protected Object ItemPresenter;
public BaseBindingAdapter(Context mContext, List mDatas, int mLayoutId) {
this.mContext = mContext;
this.mLayoutId = mLayoutId;
this.mDatas = mDatas;
this.mInfalter = LayoutInflater.from(mContext);
}
public BaseBindingAdapter(Context mContext, List mDatas) {
this.mContext = mContext;
this.mDatas = mDatas;
this.mInfalter = LayoutInflater.from(mContext);
}
@Override
public BaseBindingVH<B> onCreateViewHolder(ViewGroup parent, int viewType) {
BaseBindingVH<B> holder = new BaseBindingVH<B>((B) DataBindingUtil.inflate(mInfalter, mLayoutId, parent, false));
onCreateViewHolder(holder);
return holder;
}
/**
* 如果需要给Vh设置监听器啥的 可以在这里
*
* @param holder
*/
public void onCreateViewHolder(BaseBindingVH<B> holder) {
}
/**
* 子类除了绑定数据,还要设置监听器等其他操作。
* 可以重写这个方法,不要删掉super.onBindViewHolder(holder, position);
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(BaseBindingVH<B> holder, int position) {
holder.getBinding().setVariable(BR.data, mDatas.get(position));
holder.getBinding().setVariable(BR.itemP, ItemPresenter);
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return null != mDatas ? mDatas.size() : 0;
}
public Object getItemPresenter() {
return ItemPresenter;
}
/**
* 用于设置Item的事件Presenter
*
* @param itemPresenter
* @return
*/
public BaseBindingAdapter setItemPresenter(Object itemPresenter) {
ItemPresenter = itemPresenter;
return this;
}
/**
* 刷新数据,初始化数据
*
* @param list
*/
public void setDatas(List<D> list) {
if (this.mDatas != null) {
if (null != list) {
List<D> temp = new ArrayList<D>();
temp.addAll(list);
this.mDatas.clear();
this.mDatas.addAll(temp);
} else {
this.mDatas.clear();
}
} else {
this.mDatas = list;
}
notifyDataSetChanged();
}
/**
* 删除一条数据
* 会自动定向刷新
*
* @param i
*/
public void remove(int i) {
if (null != mDatas && mDatas.size() > i && i > -1) {
mDatas.remove(i);
notifyItemRemoved(i);
}
}
/**
* 添加一条数据 至队尾
* 会自动定向刷新
*
* @param data
*/
public void add(D data) {
if (data != null && mDatas != null) {
mDatas.add(data);
notifyItemInserted(mDatas.size());
}
}
/**
* 在指定位置添加一条数据
* 会自动定向刷新
* <p>
* 如果指定位置越界,则添加在队尾
*
* @param position
* @param data
*/
public void add(int position, D data) {
if (data != null && mDatas != null) {
if (mDatas.size() > position && position > -1) {
mDatas.add(position, data);
notifyItemInserted(position);
} else {
add(data);
}
}
}
/**
* 加载更多数据
*
* @param list
*/
public void addDatas(List<D> list) {
if (null != list) {
List<D> temp = new ArrayList<D>();
temp.addAll(list);
if (this.mDatas != null) {
this.mDatas.addAll(temp);
} else {
this.mDatas = temp;
}
notifyDataSetChanged();
}
}
public List<D> getDatas() {
return mDatas;
}
}