# DBindingView **Repository Path**: dcx/DBindingView ## Basic Information - **Project Name**: DBindingView - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2020-10-09 - **Last Updated**: 2025-02-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

Table of Contents

# 写在前面 一个对databinding的常用封装库,包含基础控件及recycleview等。不了解databinding的可以先阅读相关网上的资料,这里推荐[ Android DataBinding介绍](https://blog.csdn.net/tianjf0514/article/details/75195108) # 缩略图 ![normalWidget](https://github.com/itlwy/DBindingView/blob/master/pic/dbindingview.gif) ![recycleview](https://github.com/itlwy/DBindingView/blob/master/pic/dbindingview1.gif) # 如何引入 ## step1 Add the JitPack repository to your build file allprojects { repositories { ... maven { url 'https://jitpack.io' } } } ## step2 Add the dependency dependencies { compile 'com.github.itlwy:DBindingView:v1.1.14' } # 如何使用 ## 一般控件 如上面的动态图,目前展示了基本控件的进行一些初始化的封装,目的是将一些繁琐的初始化操作自动化掉。注意,这里用到的属性均是自定义属性,前缀请用app: ### 涉及到的类
类名 包名 说明
KeyValue com.lwy.dbindingview.data 存储key(Integer)-value(String),主要用于spinner、checkbox、radiogroup等,实现数据源的key-label模式
BindingSpinner com.lwy.dbindingview.bindingadapter.spinner 封装的双向绑定自定义Spinner
DataBindingRadioGroup com.lwy.dbindingview.bindingadapter.radiogroup 封装的双向绑定自定义RadioGroup
DataBindingRadioButton com.lwy.dbindingview.bindingadapter.radiogroup 封装的双向绑定自定义RadioButton
BindingCheckGroup com.lwy.dbindingview.bindingadapter.checkbox 封装的双向绑定自定义LinearLayout,用作BindingCheckBox容器
BindingCheckBox com.lwy.dbindingview.bindingadapter.checkbox 封装的双向绑定自定义CheckBox
BindingEditText com.lwy.dbindingview.bindingadapter.edittext.BindingEditText 让EditText支持绑定数值类型,eg:Integer、Double…
### 属性
控件 自定义属性 值类型 说明
BindingSpinner selectedValue KeyValue   绑定选中的值
  spinneritems List<KeyValue>   spinner的适配器数据源
DataBindingRadioGroup selectedValue KeyValue   绑定RadioGroup选中的值
  items List<KeyValue>   设置该属性可动态渲染子view
  childViewFactory DBCustomViewFactory<DataBindingRadioButton>   当设置了items属性时,可通过此属性传入继承自DataBindingRadioButton的自定义view,不设则用默认类
DataBindingRadioButton value KeyValue   初始化RadioButton的值
BindingCheckGroup selectedValues List<KeyValue>   存储checkbox选中的值,默认用,分割
  items List<KeyValue>   设置该属性可动态渲染子view
  childViewFactory DBCustomViewFactory<BindingCheckBox>   当设置了items属性时,可通过此属性传入继承自BindingCheckBox的自定义view,不设则用默认类
BindingCheckBox value KeyValue   设置值
ImageView uri String or ObservableField   图片的url,用的加载框架是glide
  placeholderImageRes int eg:R.mipmap.ic_launcher 占位图
  request_width、request_width int   设置图片的大小,不设置默认用view的大小,2个属性必修同时设置才有效
View clickCommand ReplyCommand   点击事件触发的命令
  display boolean   控制view的Visibility
BindingEdittext textDouble、textInt、textFloat、textLong Double、Float、Integer、Long   绑定数值类型
  regularExpression   正则表达式校验输入值 
具体使用很简单,直接看demo–>com.lwy.dbindingview.base_widget.WidgeActivity 这里说明下DataBindingRadioGroup和BindingCheckGroup, 这里的app:items属性,让其根据sexList动态渲染子view(DataBindingRadioButton),也可以选择在布局里直接写子View,如: --- BindingCheckGroup同理 ## RecycleView ### 属性
属性 值类型 说明
itemBinding ItemBinding   必填,item的布局和变量的绑定关系包装类
items List<T>   必填,数据源
adapter BindingRecyclerViewAdapter<T>   选填,可继承BindingRecyclerViewAdapter自定义适配器
itemIds BindingRecyclerViewAdapter.ItemIds<? super T>   选填,不设置则默认使用position
viewHolder BindingRecyclerViewAdapter.ViewHolderFactory   选填,可继承以实现自定义ViewHolder
onItemClick BindingRecyclerViewAdapter.OnItemClickListener   item点击事件
layoutManager LayoutManagers.LayoutManagerFactory   必填,布局方式,如线性:LayoutManagers.linear()
       
### 代码说明 1. 单个布局的简单列表代码片段 1、定义viewmodel public class ItemVM extends BaseObservable { public final boolean checkable; // for now,it's useless @Bindable private int index; @Bindable private boolean checked; public ItemVM(int index, boolean checkable) { this.index = index; this.checkable = checkable; } public int getIndex() { return index; } public boolean isChecked() { return checked; } public boolean onToggleChecked(View v) { if (!checkable) { return false; } checked = !checked; // notifyPropertyChanged(com.lwy.dbindingview.BR.checked); return true; } } 2、定义Item布局文件R.layout.item 3、创建layout和viewmodel变量的绑定关系包装类 public final ItemBinding singleItem = ItemBinding.of(com.lwy.dbindingview.BR.item, R.layout.item); 4、创建数据源 public final ObservableList items = new ObservableArrayList<>(); 5、设置RecycleView的属性 ... ... 2. 复杂布局代码片段 1、定义viewmodel(同上) 2、定义Item布局文件R.layout.item(同上) 3、创建footer的viewmodel public class FooterVM extends RcVFooterVM { public final ReplyCommand clickCommand = new ReplyCommand(new Action0() { @Override public void call() { if (!getIsFooterLoading().get()) { switchLoading(true); callback.execute(); } } }); private ReplyCommand callback; public final ObservableField noMoreTip = new ObservableField<>(); /* state : 0 loading state : 1 idle */ public final ObservableField state = new ObservableField<>(); public FooterVM(ReplyCommand callback) { super(); this.callback = callback; switchLoading(false); noMoreTip.set("暂无更多"); } @Override protected ReplyCommand geneOnLoadMoreCommand() { return new ReplyCommand<>(new Action1() { @Override public void call(Integer integer) { FooterVM.this.callback.execute(); // switchLoading(true); } }); } @Override public void switchLoading(boolean flag) { if (flag) { state.set(0); } else { state.set(1); } super.switchLoading(flag); } } 4、创建footer的布局文件R.layout.default_loading 5、创建layout和viewmodel变量的绑定关系包装类 // 这里根据class类型来控制不同的item public final ItemBinding multipleItems = ItemBinding.of(new OnItemBindClass<>() .map(FooterVM.class, BR.footerVM, R.layout.default_loading) .map(String.class, com.lwy.dbindingview.BR.item, R.layout.item_header_footer) .map(ItemVM.class, com.lwy.dbindingview.BR.item, R.layout.item)); 6、创建数据源 public final FooterVM footerVM = new FooterVM(new ReplyCommand(new Action1() { @Override public void call(Integer integer) { // 异步执行加载数据 完了需要调用 "footerVM.switchLoading(false)" 取消加载状态 } })); public final ObservableList items = new ObservableArrayList<>(); public final MergeObservableList headerFooterItems = new MergeObservableList<>() .insertItem("Header") .insertList(items) .insertItem(footerVM); 5、设置RecycleView的属性 ... ... # 参考 1. [binding-collection-adapter](https://github.com/evant/binding-collection-adapter) # License Copyright 2018 lwy 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.