RxJava and Rxandroid Four (use of rxbinding)

Source: Internet
Author: User
Tags addall

Students who are unfamiliar with Rxjava can read the articles I wrote before.

RxJava and Rxandroid One (basic)
RxJava and Rxandroid II (use of Operators)
RxJava and Rxandroid III (Life cycle control and memory optimization)

Also recommend a few good articles, to help understand Rxjava
How the Android client uses the RxJava
Implement an Event Bus–rxbus with RxJava
Play Rxjava (i) basic knowledge
RxJava Tutorial Part II: Filtering data based on the event stream
Meizhi Android Rxjava & Retrofit Best Practices

Preface : Rxbinding is an open source library of Jake Wharton, which provides a set of Rxjava-based Binding APIs on the Android platform. The so-called binding is the API for registering binding objects such as setting Onclicklistener, setting Textwatcher.

one: Git address

Https://github.com/JakeWharton/RxBinding

second, the use of Androidstudio

The general package is used below

Compile ' com.jakewharton.rxbinding:rxbinding:0.4.0 '

V4 ' SUPPORT-V4 ' Library bindings:

Compile ' com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0 '

' Appcompat-v7 ' Library bindings:

Compile ' com.jakewharton.rxbinding:rxbinding-appcompat-v7:0.4.0 '

' Design ' library bindings:

Compile ' com.jakewharton.rxbinding:rxbinding-design:0.4.0 '

Third, code example
  • Button Anti-shake processing

     Button = (button) Findviewbyid (R.ID.BT); Rxview.clicks (Button). Throttlefirst (2, Timeunit.seconds)//Two seconds to take only one click event, anti-shake operation. Subscribe (n EW action1<void> () {@Override public void call (void aVoid) {to                 Ast.maketext (Mainactivity.this, "clicked", Toast.length_short). Show (); }             }) ;
  • Long-press-time monitoring of the button

     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 Click events, long press event handling

    ListView = (ListView) Findviewbyid (R.id.listview);     list<string> list = new arraylist<> ();     for (int i = 0; i < 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, T Oast.             Length_short). Show (); }         }) ;
-User Login screen, tick the consent privacy agreement, the login button will be highlighted  
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 );            }        }) ;```


  • search, Keyword associative function. Debounce () sends an event without action within a certain amount of time.

     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 (timeunit.milliseconds). Map (New Func1<char                     Sequence, 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 li                    St 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<st                     Ring>> () {@Override public void call (list<string> strings) {                     Adapter.clear ();                     Adapter.addall (strings);                 Adapter.notifydatasetchanged (); }             }) ;

    Run effect

Summary :

  • RxBindingIt is the 发布--订阅 mode used in the Android control of the click, text changes on. After the rxbinding has converted the tap listener to Observable, it is possible to extend it.

  • RxBindingAnd rxlifecycle combined, you can control the life cycle of control monitoring. For rxlifecycle use, refer to Rxjava and Rxandroid III (lifecycle Control and memory optimization)

  • This series of code examples are all on GitHub https://github.com/zyj1609wz/RxJava_RxAndroid

  • Finally my personal public number is: zhaoyanjun125 , welcome to pay attention to

RxJava and Rxandroid Four (use of rxbinding)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.