-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
BaseView.java
executable file
·344 lines (295 loc) · 9.16 KB
/
BaseView.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
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
/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package zuo.biao.library.base;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import zuo.biao.library.util.CommonUtil;
import zuo.biao.library.util.Log;
/**基础自定义View,适合任何View
* <br /> 可以用于Adapter内的ItemView,也可以单独作为一个组件使用
* @author Lemon
* @param <T> 数据模型(model/JavaBean)类
* @see #onDataChangedListener
* @see #onDestroy
* @use extends BaseView<T>, 具体参考.DemoView
*/
public abstract class BaseView<T> extends RecyclerView.ViewHolder {
private static final String TAG = "BaseView";
/**点击事件
*/
public interface OnViewClickListener {
/**
* @param bv
* @param v
*/
void onViewClick(@NonNull BaseView bv, @NonNull View v); //BaseView<T>泛型太麻烦,不利于多层View传递
}
/**数据改变回调接口
* (Object) getData() - 改变的数据
*/
public interface OnDataChangedListener {
void onDataChanged();
}
/**
* 需要在BaseView子类手动回调 onViewClickListener.onViewClick(...)
*/
public OnViewClickListener onViewClickListener;//数据改变回调监听回调的实例
public BaseView<T> setOnViewClickListener(OnViewClickListener onViewClickListener) {
this.onViewClickListener = onViewClickListener;
return this;
}
/**
* 需要在BaseView子类手动回调 onViewClickListener.onDataChanged()
*/
public OnDataChangedListener onDataChangedListener;//数据改变回调监听回调的实例
/**设置数据改变事件监听回调
* @param listener
*/
public BaseView<T> setOnDataChangedListener(OnDataChangedListener listener) {
onDataChangedListener = listener;
return this;
}
/**
* @param context
* @param layoutResId
* @see #BaseView(Activity, int, ViewGroup)
*/
public BaseView(Activity context, @LayoutRes int layoutResId) {
this(context, layoutResId, null);
}
/**
* @param context
* @param layoutResId
* @param parent TODO 如果itemView不能占满宽度 或 高度不对,一般是RecyclerView的问题,可通过传parent解决
*/
public BaseView(Activity context, @LayoutRes int layoutResId, ViewGroup parent) {
this(context, context.getLayoutInflater().inflate(layoutResId, parent, false));
}
/**
* 传入的Activity,可在子类直接使用
*/
public final Activity context;
/**
* @param context
* @param itemView
*/
public BaseView(Activity context, View itemView) {
super(itemView);
this.context = context;
}
/**通过id查找并获取控件,使用时不需要强转
* @param id
* @return
*/
@SuppressWarnings("unchecked")
public <V extends View> V findView(int id) {
return (V) itemView.findViewById(id);
}
/**通过id查找并获取控件,使用时不需要强转
* @param id
* @return
*/
public <V extends View> V findViewById(int id) {
return findView(id);
}
/**通过id查找并获取控件,并setOnClickListener
* @param id
* @param listener
* @return
*/
public <V extends View> V findView(int id, OnClickListener listener) {
V v = findView(id);
v.setOnClickListener(listener);
return v;
}
/**通过id查找并获取控件,并setOnClickListener
* @param id
* @param listener
* @return
*/
public <V extends View> V findViewById(int id, OnClickListener listener) {
return findView(id, listener);
}
public T data = null;
/**
* data在列表中的位置
* @must 只使用bindView(int position, T data)方法来设置position,保证position与data对应正确
*/
public int position = 0;
/**
* 视图类型,部分情况下需要根据viewType使用不同layout,对应Adapter的itemViewType
*/
public int viewType = 0;
/**
* 是否已被选中
*/
public boolean selected = false;
/**创建一个新的View
* @return
*/
public View createView() {
return itemView;
}
/**获取itemView的宽度
* @warn 只能在createView后使用
* @return
*/
public int getWidth() {
return itemView.getWidth();
}
/**获取itemView的高度
* @warn 只能在createView后使用
* @return
*/
public int getHeight() {
return itemView.getHeight();
}
/**设置并显示内容,建议在子类bindView内this.data = data;
* @warn 只能在createView后使用
* @param data - 传入的数据
* @param position - data在列表中的位置
* @param viewType - 视图类型,部分情况下需要根据viewType使用不同layout
*/
public void bindView(T data, int position, int viewType) {
this.position = position;
this.viewType = viewType;
bindView(data);
}
/**设置并显示内容,建议在子类bindView内this.data = data;
* @warn 只能在createView后使用
* @param data_ - 传入的数据
*/
public void bindView(T data_) {
if (data_ == null) {
Log.w(TAG, "bindView data_ == null");
}
this.data = data_;
//不一定要用单选功能,实现也不一定要用这种方式,这里写会影响所有BaseView子类的性能,子类写更好 itemView.setSelected(selected);
}
/**获取可见性
* @warn 只能在createView后使用
* @return 可见性 (View.VISIBLE, View.GONE, View.INVISIBLE);
*/
public int getVisibility() {
return itemView.getVisibility();
}
/**设置可见性
* @warn 只能在createView后使用
* @param visibility - 可见性 (View.VISIBLE, View.GONE, View.INVISIBLE);
*/
public void setVisibility(int visibility) {
itemView.setVisibility(visibility);
}
/**设置背景
* @warn 只能在createView后使用
* @param resId
*/
public void setBackground(int resId) {
itemView.setBackgroundResource(resId);
}
// /**性能不好
// * @param id
// * @param s
// */
// public void setText(int id, String s) {
// TextView tv = (TextView) findViewById(id);
// tv.setText(s);
// }
//resources方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public Resources resources;
public final Resources getResources() {
if(resources == null) {
resources = context.getResources();
}
return resources;
}
public String getString(int id) {
return getResources().getString(id);
}
public int getColor(int id) {
return getResources().getColor(id);
}
public Drawable getDrawable(int id) {
return getResources().getDrawable(id);
}
public float getDimension(int id) {
return getResources().getDimension(id);
}
//resources方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//show short toast 方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**快捷显示short toast方法,需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
* @param stringResId
*/
public void showShortToast(int stringResId) {
CommonUtil.showShortToast(context, stringResId);
}
/**快捷显示short toast方法,需要long toast就用 Toast.makeText(string, Toast.LENGTH_LONG).show(); ---不常用所以这个类里不写
* @param string
*/
public void showShortToast(String string) {
CommonUtil.showShortToast(context, string);
}
//show short toast 方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//启动新Activity方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**打开新的Activity,向左滑入效果
* @param intent
*/
public void toActivity(final Intent intent) {
CommonUtil.toActivity(context, intent);
}
/**打开新的Activity
* @param intent
* @param showAnimation
*/
public void toActivity(final Intent intent, final boolean showAnimation) {
CommonUtil.toActivity(context, intent, showAnimation);
}
/**打开新的Activity,向左滑入效果
* @param intent
* @param requestCode
*/
public void toActivity(final Intent intent, final int requestCode) {
CommonUtil.toActivity(context, intent, requestCode);
}
/**打开新的Activity
* @param intent
* @param requestCode
* @param showAnimation
*/
public void toActivity(final Intent intent, final int requestCode, final boolean showAnimation) {
CommonUtil.toActivity(context, intent, requestCode, showAnimation);
}
//启动新Activity方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**销毁并回收内存,建议在对应的View占用大量内存时使用
* @warn 只能在UI线程中调用
*/
public void onDestroy() {
if (itemView != null) {
try {
itemView.destroyDrawingCache();
} catch (Exception e) {
Log.w(TAG, "onDestroy try { itemView.destroyDrawingCache();" +
" >> } catch (Exception e) {\n" + e.getMessage());
}
}
onDataChangedListener = null;
data = null;
position = 0;
}
}