preface

  • RxjavaBecause of itsChain call based on event flow, simple logic & easy to useThe characteristics of the deepAndroidDeveloper welcome.





Making screenshots

If you are not familiar with RxJava, check out Android: a clear and easy-to-understand introduction to RxJava

  • RxJavaThat’s why it’s so popularProvides rich & powerful operators that can fulfill almost all functional requirements
  • Today, I’m going to bring youRxjavaOf the create operatorCommon development application scenarios: Joint judgment requirementsI hope you will enjoy it.
  1. This series of articles is based onRxjava 2.0
  2. In the coming days, I will continue to publish a series of articles on Rxjava 2.0 in Android, including principles, operators, application scenarios, back pressure, etc. If you are interested, please continue to follow Carson_Ho’s Android development notes!!





Schematic diagram


directory





Schematic diagram


1. Requirement scenario

You need to make joint judgments about multiple events at the same time

For example, when filling out a form, all the information in the form (name, age, occupation, etc.) must be filled in before clicking the “Submit” button


2. Function Description

  • Fill in the form here as the joint judgment function display
  • That is, all the information in the form (name, age, occupation, etc.) has been filled in before clicking the “Submit” button is allowed

3. Implementation

  • The principle of

    usingRxJavaIn the combined operatorCombineLatest ()implementation

For more information on the use of combineLatest (), see Android RxJava: Combinatorial/merge operators

  • Concrete code implementation

activity_main.xml

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/name" Android :layout_width="wrap_content" Android :layout_height="wrap_content" Android: Hint =" Please fill in your name "/> <EditText Android :id="@+id/age" Android :layout_width="wrap_content" Android :layout_height="wrap_content" Android: Layout_height ="wrap_content" <EditText android:id="@+id/job" android:layout_width="wrap_content" android:layout_height="wrap_content" <Button android:id="@+id/list" Android :layout_width="wrap_content" Android :layout_height="wrap_content" Android :text=" Submit "Android :enabled="false" /> </LinearLayout>Copy the code

MainActivity.java

/* * Step 1: Set the control variable & bind **/ EditText name,age,job; Button list; name = (EditText) findViewById(R.id.name); age = (EditText) findViewById(R.id.age); job = (EditText) findViewById(R.id.job); list = (Button) findViewById(R.id.list); /* * Step 2: Set the observed for each EditText to send listening events * RxBinding: rxTextView.textChanges (name) = The compile 'com. Jakewharton. Rxbinding2: rxbinding: 2.0.0' * 2. Pass in the EditText control, and when any EditText is clicked to write, it sends the return value of the data event = Function3 (more on that below) * 3. Skip (1); skip(1); **/ Observable<CharSequence> nameObservable = rxTextView.textChanges (name).skip(1); Observable<CharSequence> ageObservable = RxTextView.textChanges(age).skip(1); Observable<CharSequence> jobObservable = RxTextView.textChanges(job).skip(1); /* * Step 3: Through combineLatest () merging & joint judgment * * / Observable.com bineLatest (nameObservable ageObservable, jobObservable, new Function3<CharSequence, CharSequence, CharSequence,Boolean>() { @Override public Boolean apply(@NonNull CharSequence charSequence, @nonNULL CharSequence charSequence2, @NonNULL CharSequence charSequence3) throws Exception {/* * Step 4: Form information input cannot be empty **/ / 1. Name information Boolean isUserNameValid =! TextUtils.isEmpty(name.getText()) ; // Boolean isUserNameValid =! TextUtils.isEmpty(name.getText()) && (name.getText().toString().length() > 2 && name.getText().toString().length() < 9);  Age information Boolean isUserAgeValid =! TextUtils.isEmpty(age.getText()); Boolean isUserJobValid =! TextUtils.isEmpty(job.getText()) ; Return isUserNameValid && isUserAgeValid && isUserJobValid; return isUserNameValid && isUserAgeValid && isUserJobValid; return isUserNameValid && isUserAgeValid && isUserJobValid; }}). Subscribe (new Consumer<Boolean>() {@override public void accept(Boolean s) throws Exception {/* * Step 6: subscribe(new Consumer<Boolean>() {@override public void accept(Boolean s) throws Exception {/* * Step 6: subscribe Return result & Set button clickable style **/ log. e(TAG, "Submit button clickable: "+s); list.setEnabled(s); }});Copy the code
  • The test results





Schematic diagram


4. The Demo address

Carson_Ho Github address = RxJava2


5. To summarize

  • This article mainly explainsRxjavaThe actual development requirements scenario: joint judgment
  • Now I’m going to combineActual scenario application &RxjavaRelevant use frameworks (e.gRetrofit,Eventbus), continue toAndroidIn theRxjavaThe actual development requirements scenario for in-depth explanation, interested can continue to pay attention toCarson_Ho android Development Notes





Schematic diagram


Thumb up, please! Because your encouragement is the biggest power that I write!

Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: Android RxJava network request polling Android RxJava network request nested callback Android RxJava network request nested callback Retrieve cached data from disk/memory caches


Welcome to attentionCarson_HoJane books!

Share the dry things about Android development from time to time, the pursuit of short, flat, fast, but there is no lack of depth.