The path to Android development and learning-experience at the beginning of RxAndroid

Source: Internet
Author: User

The path to Android development and learning-experience at the beginning of RxAndroid

After learning android for a while, I read some of the project code, and then I think it is boring to learn the basics. So I will learn new things, I believe that many java learners have heard of RxJava, so RxAndroid is also available in android.

The two core aspects of RxJava are Observables (Observer, event source) and Subscribers (subscriber ). The Observables issue a series of events and Subscribers processes these events. Events here can be anything you are interested in, touch events, data returned by web interface calls, and so on.

For details about RxAndroid github: https://github.com/reactivex/rxandroid.

Create an emRxAndroidStudy project and add it to dependencies of build. grade:

 

    compile 'io.reactivex:rxandroid:1.1.0'    compile 'io.reactivex:rxjava:1.1.0'

Next, let's try RxAndroid. First, we will use the method of the previous Annotation to copy four Annotation-related files to the project, and write the MainActivity Code as follows:
package com.jared.emrxandroidstudy;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import rx.Observable;import rx.Subscriber;@EMLayoutBinder(R.layout.activity_main)public class MainActivity extends BaseActivity {    private static final String TAG = "MainActivity";    private Subscriber
 
   subscriber;    private Observable
  
    observable;    @EMViewBinder(R.id.hello)    private TextView mHello;    @EMViewBinder(R.id.test1)    private Button mTest1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        createSubscriber();    }    private void bindSubscriber() {        observable.subscribe(subscriber);    }    private void createSubscriber() {        subscriber = new Subscriber
   
    () {            @Override            public void onCompleted() {                Log.d(TAG, "onCompleted");            }            @Override            public void onError(Throwable e) {                e.printStackTrace();            }            @Override            public void onNext(String t) {                Log.d(TAG, "onNext");                mHello.setText(t);            }        };    }    private String getHello() {        return "Hello RxAndroid";    }    private String getHello1() {        return "Hello RxAndroid 1";    }    @EMOnClickBinder({R.id.test1})    public void myOnClick(View view) {        switch (view.getId()) {            case R.id.test1:                createObservable();                break;            default:                break;        }    }    private void createObservable() {        Log.d(TAG, "observable");        observable = Observable.create(new Observable.OnSubscribe
    
     () {            @Override            public void call(Subscriber
      subscriber) {                subscriber.onNext(getHello());                subscriber.onCompleted();            }        });        bindSubscriber();    }}
    
   
  
 
The layout file is as follows:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jared.emrxandroidstudy.MainActivity">    <textview android:id="@+id/hello" android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content"><button android:id="@+id/test1" android:text="Test" android:layout_width="match_parent" android:layout_height="wrap_content" android:textallcaps="false"></button></textview></linearlayout>

Here, the Observable is created to send a string, and then the Subscriber is created to receive event processing, and then bind the two. After the button is pressed, the subscriber will call the onNext method and the onCompleted method.

Of course, the createObservable can be simplified using the just method:

 

 private void createObservableByJust() {        Log.d(TAG, "createObservable");        observable = Observable.just(getHello());        bindSubscriber();    }
The effect is the same as above. Next we simplified the subscriber:

 

 

private void createSubscriberByAction() {        onNextAction = new Action1
 
  () {            @Override            public void call(String s) {                mHello.setText(s);            }        };    }
 

This is implemented through Action1, and the feeling of subscriber is completely lost. Then, modify the binding as follows:
 private void bindSubscriber() {        //observable.subscribe(subscriber);        observable.subscribe(onNextAction);    }
The results are the same.

 

Now let's use the map operator and modify it as follows:

 

 private void createObservableByMap() {        Log.d(TAG, "createObservableByMap");        Observable.just(getHello()).map(new Func1
 
  () {            @Override            public String call(String s) {                return s + " by eastmoon";            }        }).subscribe(onNextAction);    }
 

After running the result, the original string is added by eastmoon. In fact, the function of map is to operate data between observable and subscribe.

Related Article

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.