11
11
添加依赖:
12
12
13
13
``` gradle
14
- implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-base:1.2.6 '
14
+ implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-base:2.0.0 '
15
15
```
16
16
17
17
改造的核心步骤:
@@ -99,67 +99,7 @@ class HomeFragment extends BaseBindingFragment<FragmentHomeBinding> {
99
99
100
100
### Adapter
101
101
102
- 下面提供两个适配器开源库的封装改造示例,如果你有在用这两个库,可以直接拷贝去用。如果是自己封装的适配器基类,可参考下面封装的思路进行改造。
103
-
104
- - #### [ BaseRecyclerViewAdapterHelper] ( https://github.com/CymChad/BaseRecyclerViewAdapterHelper )
105
-
106
- 封装基类代码:
107
-
108
- ``` java
109
- public abstract class BaseBindingQuickAdapter <T, VB extends ViewBinding >
110
- extends BaseQuickAdapter<T , BaseBindingQuickAdapter .BaseBindingHolder > {
111
-
112
- public BaseBindingQuickAdapter () {
113
- this (- 1 );
114
- }
115
-
116
- public BaseBindingQuickAdapter (@LayoutRes int layoutResId ) {
117
- super (layoutResId);
118
- }
119
-
120
- @NotNull
121
- @Override
122
- protected BaseBindingHolder onCreateDefViewHolder (@NotNull ViewGroup parent , int viewType ) {
123
- VB viewBinding = ViewBindingUtil . inflateWithGeneric(this , parent);
124
- return new BaseBindingHolder (viewBinding);
125
- }
126
-
127
- public static class BaseBindingHolder extends BaseViewHolder {
128
-
129
- private final ViewBinding binding;
130
-
131
- public BaseBindingHolder (@NotNull View view ) {
132
- this (() - > view);
133
- }
134
-
135
- public BaseBindingHolder (@NotNull ViewBinding binding ) {
136
- super (binding. getRoot());
137
- this . binding = binding;
138
- }
139
-
140
- @NonNull
141
- @SuppressWarnings (" unchecked" )
142
- public <VB extends ViewBinding > VB getViewBinding () {
143
- return (VB ) binding;
144
- }
145
- }
146
- }
147
- ```
148
-
149
- 封装后的使用示例:
150
-
151
- ``` java
152
- class FooAdapter extends BaseBindingQuickAdapter<Foo , ItemFooBinding > {
153
-
154
- @Override
155
- public void convert (@NotNull BindingViewHolder<ItemFooBinding > holder , Foo item ) {
156
- ItemFooBinding binding = holder. getViewBinding();
157
- binding. tvFoo. setText(item. getValue());
158
- }
159
- }
160
- ```
161
-
162
- - #### [ MultiType] ( https://github.com/drakeet/MultiType )
102
+ 下面是第三方适配器库 [ MultiType] ( https://github.com/drakeet/MultiType ) 的封装示例,如果是自己封装的适配器基类,可参考下面的封装思路进行改造。
163
103
164
104
封装基类代码:
165
105
@@ -172,6 +112,13 @@ public abstract class BindingViewDelegate<T, VB extends ViewBinding> extends
172
112
return new BindingViewHolder<> (ViewBindingUtil . inflateWithGeneric(this , parent));
173
113
}
174
114
115
+ @Override
116
+ public void onBindViewHolder (@NonNull BindingViewHolder<VB > holder , T t ) {
117
+ onBindViewHolder(holder. getBinding(), t, holder. getAdapterPosition());
118
+ }
119
+
120
+ protected abstract void onBindViewHolder (VB binding , T item , int position );
121
+
175
122
public static class BindingViewHolder <VB extends ViewBinding > extends RecyclerView .ViewHolder {
176
123
177
124
private final VB binding;
@@ -195,8 +142,8 @@ public abstract class BindingViewDelegate<T, VB extends ViewBinding> extends
195
142
class FooViewDelegate extends BindingViewDelegate<Foo , ItemFooBinding > {
196
143
197
144
@Override
198
- public void onBindViewHolder (@NotNull BindingViewHolder< ItemFooBinding > holder , Foo item ) {
199
- holder . getBinding() . tvFoo. setText(item. getValue());
145
+ protected void onBindViewHolder (@NotNull ItemFooBinding binding , Foo foo , int position ) {
146
+ binding . tvFoo. setText(item. getValue());
200
147
}
201
148
}
202
149
```
@@ -300,72 +247,7 @@ public class HomeFragment extends BaseBindingFragment<FragmentHomeBinding> {
300
247
301
248
### Adapter
302
249
303
- 下面提供两个适配器开源库的封装改造示例,如果你有在用这两个库,可以直接拷贝去用。如果是自己封装的适配器基类,可参考下面封装的思路进行改造。
304
-
305
- - #### [ BaseRecyclerViewAdapterHelper] ( https://github.com/CymChad/BaseRecyclerViewAdapterHelper )
306
-
307
- 封装基类代码:
308
-
309
- ``` java
310
- public abstract class BaseBindingQuickAdapter <T, VB extends ViewBinding >
311
- extends BaseQuickAdapter<T , BaseBindingQuickAdapter .BaseBindingHolder > {
312
-
313
- public BaseBindingQuickAdapter () {
314
- this (- 1 );
315
- }
316
-
317
- public BaseBindingQuickAdapter (@LayoutRes int layoutResId ) {
318
- super (- layoutResId);
319
- }
320
-
321
- @NotNull
322
- @Override
323
- protected BaseBindingHolder onCreateDefViewHolder (@NotNull ViewGroup parent , int viewType ) {
324
- return new BaseBindingHolder (onCreateViewBinding(LayoutInflater . from(parent. getContext()), parent));
325
- }
326
-
327
- protected abstract VB onCreateViewBinding (@NonNull LayoutInflater inflater , @NonNull ViewGroup parent );
328
-
329
- public static class BaseBindingHolder extends BaseViewHolder {
330
-
331
- private final ViewBinding binding;
332
-
333
- public BaseBindingHolder (@NotNull View view ) {
334
- this (() - > view);
335
- }
336
-
337
- public BaseBindingHolder (@NotNull ViewBinding binding ) {
338
- super (binding. getRoot());
339
- this . binding = binding;
340
- }
341
-
342
- @NonNull
343
- @SuppressWarnings (" unchecked" )
344
- public <VB extends ViewBinding > VB getViewBinding () {
345
- return (VB ) binding;
346
- }
347
- }
348
- }
349
- ```
350
-
351
- 封装后的使用示例:
352
-
353
- ``` java
354
- public class FooAdapter extends BaseBindingQuickAdapter<Foo , ItemFooBinding > {
355
- @Override
356
- protected ItemFooBinding onCreateViewBinding (@NonNull LayoutInflater inflater , @NonNull ViewGroup parent ) {
357
- return ItemFooBinding . inflate(inflater, parent, false );
358
- }
359
-
360
- @Override
361
- protected void convert (@NotNull BaseBindingHolder holder , Foo item ) {
362
- ItemFooBinding binding = holder. getViewBinding();
363
- binding. tvFoo. setText(item. getValue());
364
- }
365
- }
366
- ```
367
-
368
- - #### [ MultiType] ( https://github.com/drakeet/MultiType )
250
+ 下面是第三方适配器库 [ MultiType] ( https://github.com/drakeet/MultiType ) 的封装示例,如果是自己封装的适配器基类,可参考下面的封装思路进行改造。
369
251
370
252
封装基类代码:
371
253
@@ -378,8 +260,15 @@ public abstract class BindingViewDelegate<T, VB extends ViewBinding> extends
378
260
return new BindingViewHolder<> (onCreateViewBinding(LayoutInflater . from(parent. getContext()), parent));
379
261
}
380
262
263
+ @Override
264
+ public void onBindViewHolder (@NonNull BindingViewHolder<VB > holder , T t ) {
265
+ onBindViewHolder(holder. getBinding(), t, holder. getAdapterPosition());
266
+ }
267
+
381
268
protected abstract VB onCreateViewBinding (@NonNull LayoutInflater inflater , @NonNull ViewGroup parent );
382
269
270
+ protected abstract void onBindViewHolder (VB binding , T item , int position );
271
+
383
272
public static class BindingViewHolder <VB extends ViewBinding > extends RecyclerView .ViewHolder {
384
273
385
274
private final VB binding;
@@ -407,8 +296,8 @@ public class FooViewDelegate extends BindingViewDelegate<Foo, ItemFooBinding> {
407
296
}
408
297
409
298
@Override
410
- public void onBindViewHolder (@NotNull BindingViewHolder< ItemFooBinding > holder , Foo foo ) {
411
- holder . getBinding() . tvFoo. setText(item. getValue());
299
+ protected void onBindViewHolder (@NotNull ItemFooBinding binding , Foo foo , int position ) {
300
+ binding . tvFoo. setText(item. getValue());
412
301
}
413
302
}
414
303
```
0 commit comments