通过RxBinding我们可以将View的事件转化为事件流,然后就可以采用RxJava发射事件流,操作符进行操作。
RxBinding项目地址
RxBinding2源码解析
RxBinding2(二)MaterialDesign系列
RxBinding2(三)Support-V4库和RecyclerView
全部依赖库support支持:
//基础库
implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-design:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-recyclerview-v7:2.2.0'
//V4,V7对应于android的V4,V7包组件
implementation 'com.jakewharton.rxbinding2:rxbinding-support-v4:2.2.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.2.0'
//电视相关
implementation 'com.jakewharton.rxbinding2:rxbinding-leanback-v17:2.2.0'
每一个rxbinding2依赖库都以一个xxx-kotlin支持版本
settings.gragle中include ':app', ':rxbinding'
实战:
全部RxView事件,注意防内存泄漏,要么将订阅添加到CompositeDisposable,在onDestory中clear,要么采用RxLifecycle控制生命周期。
xxxEvent:因RxJava方法只有一个参数,所以xxxEvent里面封装了多个参数的情况。
handle:返回false则拦截事件,不发送事件
//基础库可以得到基础组件
implementation 'com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha1'
private void RxView(){
View mView = new View(this);
//设置是否可见
RxView.visibility(mView).accept(mBoolean);
//设置是否可点击
RxView.clickable(mView).accept(mBoolean);
//设置是否可用
RxView.enabled(mView).accept(mBoolean);
//设置是否选中
RxView.selected(mView).accept(mBoolean);
//设置是否按下
RxView.pressed(mView).accept(mBoolean);
//设置是否激活
RxView.activated(mView).accept(mBoolean);
//View被添加
RxView.attaches(mView);
//View被添加时触发
RxView.attachEvents(mView);
//View被移除
RxView.detaches(mView);
//点击事件
RxView.clicks(mView);
//长点击事件
RxView.longClicks(mView);
//被拖拽
RxView.drags(mView);
RxView.drags(mView, new Predicate<DragEvent>());
//View被重绘
RxView.draws(mView);
//焦点发生改变
RxView.focusChanges(mView);
//全局布局改变或某View可视状态改变
RxView.globalLayouts(mView);
//悬停事件
RxView.hovers(mView);
//按键被按下
RxView.keys(mView);
//组件布局参数发生改变
RxView.layoutChangeEvents(mView)
//组件布局参数发生改变
RxView.layoutChanges(mView);
//将要绘制时
RxView.preDraws();
//滚动事件API23
RxView.scrollChangeEvents();
//状态栏和导航栏可见和隐藏
RxView.systemUiVisibilityChanges(mView);
//触摸事件
RxView.touches(mView);
}
其他基础组件:
//菜单
RxMenuItem.clicks(menuItem)
RxMenuItem.actionViewEvents(menuItem)
//容器View改变
RxViewGroup.changeEvents(mViewGroup)
//绝对视图滚动事件
RxAbsListView.scrollEvents(mAbsListView)
/适配器Adapter数据改变
RxAdapter.dataChanges(mAdapter)
//承载Adapter的View
RxAdapterView.itemClicks(mAdapterView < mAdapter >)
RxAdapterView.itemClickEvents(mAdapterView < mAdapter >)
RxAdapterView.itemLongClicks(mAdapterView < mAdapter >);
RxAdapterView.itemLongClicks(mAdapterView < mAdapter >, handle);
RxAdapterView.itemLongClickEvents(mAdapterView < mAdapter >);
RxAdapterView.itemLongClickEvents(mAdapterView < mAdapter >, handle);
//自动匹配完成搜索框
RxAutoCompleteTextView.itemClickEvents(mAutoCompleteTextView)
//具有两种状态的组件状态切换
RxCompoundButton.checkedChanges(mCompoundButton)
//弹出窗体点击,消失
RxPopupMenu.itemClicks(mPopupMenu)
RxPopupMenu.dismisses(mPopupMenu)
//RadioButton勾选事件
RxRadioGroup.checked(mRadioGroup).accept(true);
RxRadioGroup.checkedChanges(mRadioGroup)
//星级滚动条进度改变事件
RxRatingBar.ratingChanges(mRatingBar)
RxRatingBar.ratingChangeEvents(mRatingBar)
//搜索框
RxSearchView.query(mSearchView, isSumbmit).accept(true);
RxSearchView.queryTextChanges(mSearchView)
RxSearchView.queryTextChangeEvents(mSearchView)
//进度条进度改变
RxSeekBar.changes(mSeekBar)
RxSeekBar.changeEvents(mSeekBar)
RxSeekBar.systemChanges(mSeekBar)
RxSeekBar.userChanges(mSeekBar)
//文本改变
RxTextView.textChanges(mTextView)
RxTextView.textChangeEvents(mTextView)
RxTextView.textChangeEvents(mTextView);
RxTextView.textChangeEvents(mTextView, handle);
RxTextView.afterTextChangeEvents(mTextView, handle);
RxTextView.beforeTextChangeEvents(mTextView, handle);
//回车事件
RxTextView.editorActions(mTextView)
RxTextView.editorActions(mTextView, handle);
//Toolbar中有五个子项和导航点击事件
RxToolbar.itemClicks(mToolBar)
RxToolbar.navigationClicks(mToolBar)
举几个例子
//View被添加时触发
RxView.attaches(mView)
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) {
}
});
//View被添加时触发Events中封装了view
RxView.attachEvents(mView)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ViewAttachEvent>() {
@Override
public void accept(ViewAttachEvent viewAttachEvent) throws Exception {
//viewAttachEvent里面封装了View
viewAttachEvent.getView();
}
});
//点击事件
RxView.clicks(mView)
.throttleFirst(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) {
}
});
//被拖拽
RxView.drags(mView)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<DragEvent>() {
@Override
public void accept(DragEvent dragEvent) throws Exception {
//DragEvent封装了各种参数
dragEvent.describeContents();
dragEvent.getAction();
dragEvent.getClipData();
dragEvent.getLocalState();
dragEvent.getClipDescription();
dragEvent.getResult();
dragEvent.getX();
dragEvent.getY();
}
});
//组件布局参数发生改变
RxView.layoutChangeEvents(mView)
.subscribe(new Consumer<ViewLayoutChangeEvent>() {
@Override
public void accept(ViewLayoutChangeEvent viewLayoutChangeEvent) throws Exception {
//ViewLayoutChangeEvent封装的参数
viewLayoutChangeEvent.getBottom();
viewLayoutChangeEvent.getLeft();
viewLayoutChangeEvent.getRight();
viewLayoutChangeEvent.getTop();
viewLayoutChangeEvent.getOldBottom();
viewLayoutChangeEvent.getOldLeft();
viewLayoutChangeEvent.getOldRight();
viewLayoutChangeEvent.getOldTop();
viewLayoutChangeEvent.getView();
viewLayoutChangeEvent.component1();
}
});
}
```java