Android Development Learning Path--rxandroid First Experience

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px">Learn a period of time android, read part of the project code, and then think about the basics are always boring and boring, then learn to learn new things, I believe a lot of Java have heard of rxjava, then Android also has RXANDROID.</span></p></p><p><p><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:Arial">Rxjava's core Two things are observables (observer, event Source) and Subscribers (subscribers). Observables emits a series of events that subscribers handle these Events. The events here can be anything you're interested in, touch events, data returned by a web interface call, and so On.</span><br></span></p></p><p><p><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:Arial">About the github:https://github.com/reactivex/rxandroid of Rxandroid.</span></span></p></p><p><p><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:Arial">Create a new Emrxandroidstudy project, and then</span></span> <span style="font-family:Arial; font-size:18px; color:#333333">Build.grade in the dependencies add:</span></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333"></span></p></p><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_1_3803553" name="code" class="java"><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_1_3803553" name="code" class="java"> Compile ' io.reactivex:rxandroid:1.1.0 ' compile ' io.reactivex:rxjava:1.1.0 '</pre></pre><br><span style="font-size:18px"><span style="font-size:18px">then we try to rxandroid, first here we still use the last annotation method, the 4 and annotation related files copied to the project, writing mainactivity code as follows:</span></span><p><p></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333"></span></p></p><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_2_9103470" name="code" class="java">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 FINA L String TAG = "mainactivity"; Private Subscriber<string> subscriber; Private Observable<string> 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<string> () {@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<string> () {@Override public void CA ll (subscriber<? Super string> subscriber) {subscriber.onnext (gethello ()); Subscriber.oncompleted ();} }); Bindsubscriber (); }}</pre><br><span style="font-size:18px">the <span style="font-size:18px">layout file is as Follows:</span></span><p><p></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333"></span></p></p><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_3_357990" name="code" class="html"><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_3_357990" name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><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"/></linearlayout></pre></pre><br><p><p></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:18px">The observable is created here to send a string, and then create the subscriber, which is used to receive event handling, and then the two bindings are pressed, and subscriber calls the OnNext method and oncompleted Method.</span></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333">of course, the createobservable here can be simplified by the just method:</span></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333"></span></p></p><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_4_499694" name="code" class="java"><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_4_499694" name="code" class="java">private void Createobservablebyjust () { log.d (TAG, "createobservable"); Observable = Observable.just (gethello ()); Bindsubscriber (); }</pre></pre><span style="font-size:18px">The effect is the same as <span style="font-size:18px">Above. Then simplify the Subscriber:</span></span><p><p></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333"></span></p></p><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_5_2069857" name="code" class="java"><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_5_2069857" name="code" class="java">private void Createsubscriberbyaction () { onnextaction = new action1<string> () { @Override Public void call (String S) { mhello.settext (s);}} ; }</pre></pre><br><span style="font-size:18px"><span style="font-size:18px">here through the Action1 to achieve, completely without the feeling of subscriber, and then modify the binding as Follows:</span></span><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_6_2767968" name="code" class="java"><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_6_2767968" name="code" class="java">private void Bindsubscriber () { //observable.subscribe (subscriber); Observable.subscribe (onnextaction); }</pre></pre><span style="font-size:18px"><span style="font-size:18px">The effect is still the Same. </span></span><p><p></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333">ok, then let's use the following operator map, which is modified as Follows:</span></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333"></span></p></p><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_7_85773" name="code" class="java"><pre code_snippet_id="1606721" snippet_file_name="blog_20160311_7_85773" name="code" class="java">private void Createobservablebymap () { log.d (TAG, "createobservablebymap"); Observable.just (gethello ()). map (new func1<string, string> () { @Override public String call (string S) { return s + "by eastmoon"; } }). Subscribe (onnextaction); }</pre></pre><br><span style="font-size:18px"><span style="font-size:18px">after running the result the original string was added by Eastmoon. In fact, the function of map is that the data can be manipulated between observable and Subscribe. </span></span><p><p></p></p><p><p><span style="font-family:Arial; font-size:18px; color:#333333"></span></p></p><p><p>Android Development Learning Path--rxandroid First Experience</p></p></span>

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.