RxBinding是JakeWharton的一個開源庫 , 它提供了一套基于RxJava的BindingApI. 可以幫助我們簡化控件/視圖添加的觸發的響應事件,而且使用起來非常簡單
點選打開連結
在build.gradle中引用以下檔案
compile 'com.jakewharton.rxbinding:rxbinding:1.0.0'
compile 'com.jakewharton.rxbinding:rxbinding-support-v4:1.0.0'
compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:1.0.0'
compile 'com.jakewharton.rxbinding:rxbinding-design:1.0.0'
<b>[html]</b> view plain copy
but = (Button) findViewById( R.id.bt) ;
RxView.clicks(but)
//表示在2秒之内隻取一個點選事件,防抖操作
.throttleFirst(2, TimeUnit.SECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
Toast.makeText(mContext, "點選了....", Toast.LENGTH_SHORT).show();
}
});
RxView.longClicks(but).subscribe(new Action1<Void>() {
Toast.makeText(mContext, "long click...", Toast.LENGTH_SHORT).show();
cb = (CheckBox) findViewById( R.id.checkbox );
RxCompoundButton.checkedChanges(cb).subscribe(new Action1<Boolean>() {
public void call(Boolean aBoolean) {
Toast.makeText(mContext, aBoolean? "被選中":"取消選擇", Toast.LENGTH_SHORT).show();
listView = (ListView) findViewById( R.id.listview );
RxAdapterView.itemClicks( listView )
.subscribe(new Action1<Integer>() {
public void call(Integer integer) {
Toast.makeText(ListActivity.this, "item click " + integer , Toast.LENGTH_SHORT).show();
}
}) ;
RxAdapterView.itemLongClicks( listView)
.subscribe(new Action1<Integer>() {
public void call(Integer integer) {
Toast.makeText(ListActivity.this, "item long click " + integer ,Toast.LENGTH_SHORT).show();
}
editText = (EditText) findViewById( R.id.editText );
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1 ); listView.setAdapter( adapter );
RxTextView.textChanges( editText )
.debounce( 600 , TimeUnit.MILLISECONDS )
.map(new Func1<CharSequence, String>() {
@Override
public String call(CharSequence charSequence) {
String key = charSequence.toString() ;
return key ;
}
})
.observeOn( Schedulers.io() )
.map(new Func1<String, List<String>>() {
public List<String> call(String keyWord ) {
List<String> dataList = new ArrayList<String>() ;
if ( ! TextUtils.isEmpty( keyWord )){
for ( String s : getData() ) {
if (s != null) {
if (s.contains(keyWord)) {
dataList.add(s);
}
}
}
}
return dataList ;
}
})
.observeOn( AndroidSchedulers.mainThread() )
.subscribe(new Action1<List<String>>() {
@Override
public void call(List<String> strings) {
adapter.clear();
adapter.addAll( strings );
adapter.notifyDataSetChanged();
}
But3 = (Button) findViewById( R.id.bt3) ;
mVerifyCodeObservable = RxView.clicks(but3)
.throttleFirst(20, TimeUnit.SECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.doOnNext(new Action1<Void>() {
@Override
public void call(Void aVoid) {
RxView.enabled(but3).call(false);
});
mVerifyCodeObservable.subscribe(new Action1<Void>() {
Observable.interval(1,TimeUnit.SECONDS,AndroidSchedulers.mainThread())
.take(20)
.subscribe(new Observer<Long>() {
@Override
public void onCompleted() {
RxTextView.text(but3).call("擷取驗證碼");
RxView.enabled(but3).call(true);
public void onError(Throwable e) {
public void onNext(Long aLong) {
RxTextView.text(but3).call("剩餘" + (20-aLong));
});
RxBinding把釋出--訂閱的模式用在了控件的點選和文本的變化上
RxBinding幾乎支援工作中常用的所有控件以及事件
RxBinding引用庫還有對應的kotlin支援庫