RxJava 和 RxAndroid 一 (基礎)
RxJava 和 RxAndroid 二(操做符的使用)
RxJava 和 RxAndroid 三(生命週期控制和內存優化)html
另外推薦幾篇比較好的文章,有助於理解Rxjava
安卓客戶端是如何使用 RxJava 的
經過 RxJava 實現一個 Event Bus – RxBus
玩透RxJava(一)基礎知識
RxJava 教程第二部分:事件流基礎之 過濾數據
Meizhi Android之RxJava & Retrofit最佳實踐java
前言:RxBinding 是 Jake Wharton 的一個開源庫,它提供了一套在 Android 平臺上的基於 RxJava的 Binding API。所謂 Binding,就是相似設置 OnClickListener 、設置 TextWatcher 這樣的註冊綁定對象的 API。android
https://github.com/JakeWharton/RxBindinggit
通常的包下面就用github
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'web
v4'support-v4' library bindings:微信
compile 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'app
'appcompat-v7' library bindings:ide
compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:0.4.0'post
'design' library bindings:
compile 'com.jakewharton.rxbinding:rxbinding-design:0.4.0'
Button 防抖處理
button = (Button) findViewById( R.id.bt ) ; RxView.clicks( button ) .throttleFirst( 2 , TimeUnit.SECONDS ) //兩秒鐘以內只取一個點擊事件,防抖操做 .subscribe(new Action1<Void>() { @Override public void call(Void aVoid) { Toast.makeText(MainActivity.this, "點擊了", Toast.LENGTH_SHORT).show(); } }) ;
按鈕的長按時間監聽
button = (Button) findViewById( R.id.bt ) ; //監聽長按時間 RxView.longClicks( button) .subscribe(new Action1<Void>() { @Override public void call(Void aVoid) { Toast.makeText(MainActivity.this, "long click !!", Toast.LENGTH_SHORT).show(); } }) ;
listView 的點擊事件、長按事件處理
listView = (ListView) findViewById( R.id.listview ); List<String> list = new ArrayList<>() ; for ( int i = 0 ; i < 40 ; i++ ){ list.add( "sss" + i ) ; } final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1 ); adapter.addAll( list ); listView.setAdapter( adapter ); //item click event RxAdapterView.itemClicks( listView ) .subscribe(new Action1<Integer>() { @Override public void call(Integer integer) { Toast.makeText(ListActivity.this, "item click " + integer , Toast.LENGTH_SHORT).show(); } }) ; //item long click RxAdapterView.itemLongClicks( listView) .subscribe(new Action1<Integer>() { @Override public void call(Integer integer) { Toast.makeText(ListActivity.this, "item long click " + integer , Toast.LENGTH_SHORT).show(); } }) ;
- 用戶登陸界面,勾選贊成隱私協議,登陸按鈕就變高亮
button = (Button) findViewById( R.id.login_bt ); checkBox = (CheckBox) findViewById( R.id.checkbox ); RxCompoundButton.checkedChanges( checkBox ) .subscribe(new Action1<Boolean>() { @Override public void call(Boolean aBoolean) { button.setEnabled( aBoolean ); button.setBackgroundResource( aBoolean ? R.color.button_yes : R.color.button_no ); } }) ; ```
效果圖
搜索的時候,關鍵詞聯想功能 。debounce()在必定的時間內沒有操做就會發送事件。
editText = (EditText) findViewById( R.id.editText ); listView = (ListView) findViewById( R.id.listview ); 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) { //get the keyword String key = charSequence.toString() ; return key ; } }) .observeOn( Schedulers.io() ) .map(new Func1<String, List<String>>() { @Override public List<String> call(String keyWord ) { //get list 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(); } }) ;
運行效果
總結:
RxBinding
就是把 發佈--訂閱
的模式用在了android控件的點擊,文本變化上。經過 RxBinding 把點擊監聽轉換成 Observable 以後,就有了對它進行擴展的可能。
RxBinding
和rxlifecycle
結合起來使用,能夠控制控件監聽的生命週期。關於rxlifecycle
的使用,請參照RxJava 和 RxAndroid 三(生命週期控制和內存優化)
這個系列的代碼示例都在 github https://github.com/zyj1609wz/RxJava_RxAndroid
最後個人我的微信公衆號是:zhaoyanjun125
, 歡迎關注