Rxjava Common usage examples

Source: Internet
Author: User

I'm not going to tell you exactly how to use it. I'll just upload the case.

Package Com.demo.testRxjava;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.support.annotation.Nullable;

Import Android.view.View;
Import Com.demo.LogUtils;
Import COM.DEMO.R;

Import Com.demo.bean.School;
Import java.io.Serializable;
Import java.util.ArrayList;
Import java.util.List;

Import Java.util.concurrent.TimeUnit; Import Rx.
observable; Import Rx.
Observer; Import Rx.
subscriber;
Import Rx.android.schedulers.AndroidSchedulers;
Import Rx.functions.Action1;
Import rx.functions.Func1;

Import Rx.schedulers.Schedulers;
 /** * Created by Pangw on 2017/12/21. */public class Testrxjava extends activity {@Override protected void onCreate (@Nullable Bundle Savedinstancesta
        TE) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_rxjava); Findviewbyid (R.ID.BTN). Setonclicklistener (New View.onclicklistener () {@Override public void oncli
          CK (view view) {iniClick17 ();  }
        }); /** * Filtering filter Filter/private void IniClick17 () {observable.just (1,2,3,4,5,6). f Ilter (New Func1<integer, boolean> () {@Override public Boolean call (Integer
                    Integer) {return integer>3;
                    }). Subscribe (New action1<integer> () {@Override
                    public void Call (Integer integer) {logutils.a (integer);

    }
                }); /** * Distinct: Remove duplicate items, better understand: * * private void IniClick16 () {Observable.just ("1", "2", 3, "3", "2")
                    , "5", "6", "4"). Distinct (). Subscribe (New action1<serializable> () { @Override public void Call (Serializable Serializable) {LOGUTILS.A (Seriali
                    zable);
       }         }); /** * Take: Before the launch of n data, or with the above example, assuming that you do not change the name of all schools, the first four schools to change the name: * * private void iniClick15 () {list<
        school> list = new arraylist<> ();
            for (int i=0;i<5;i++) {School School = new School ();
            School.setname ("name" +i);
            list<school.student> students = new arraylist<> ();
                for (int y=0;y<=i;y++) {school.student Student = new School.student ();
                Student.setname ("Student name" +i+ "-" +y);
            Students.add (student);
            } school.setstudentlist (students);
        List.add (school);
                    } observable.from (list). Take (3). Map (new Func1<school, school> () { @Override Public School Call (School School) {school.setname ("Hip Hop ha Da
                        Learning ");
                    return school; }). subscrIbe (new action1<school> () {@Override public void call (School School) {}
        });
                    Observable.from (list). Subscribe (New action1<school> () {@Override
                    public void Call (School School) {LOGUTILS.A (School.getname ());


    }
                }); /** * Map and buffer are combined using/private void IniClick14 () {list<school> List = new Arraylis
        T<> ();
            for (int i=0;i<5;i++) {School School = new School ();
            School.setname ("name" +i);
            list<school.student> students = new arraylist<> ();
                for (int y=0;y<=i;y++) {school.student Student = new School.student ();
                Student.setname ("Student name" +i+ "-" +y);
            Students.add (student);
            } school.setstudentlist (students); List.aDD (school);
                    Observable.from (list). Map (new Func1<school, school> () {@Override
                        Public School called (School School) {school.setname ("NB University");
                    return school; ). Buffer (List.size ()). Subscribe (New action1<list<school>>

                    () {@Override public void call (list<school> schools) {

    }
                }); /** * Buffer: Cache, you can set the cache size, cache full, the data sent out in the list; Example: * * private void IniClick13 () {Observable.ju
                    St (1,2,3,4,5). Buffer (2). Subscribe (New action1<list<integer>> () { @Override public void call (list<integer> integers) {LOGUTILS.A
                    (Integers.size ()); }
                });
    /** * Flagmap * If the name of the student in the country is printed * Anyway, flagmap is it. One observer translates to another observer * * private void I
        NiClick12 () {list<school> List = new arraylist<> ();
            for (int i=0;i<5;i++) {School School = new School ();
            School.setname ("name" +i);
            list<school.student> students = new arraylist<> ();
                for (int y=0;y<=i;y++) {school.student Student = new School.student ();
                Student.setname ("Student name" +i+ "-" +y);
            Students.add (student);
            } school.setstudentlist (students);
        List.add (school);
                    } observable.from (list) Flatmap (new Func1<school, observable<school.student>> () {
                        @Override Public observable<school.student> Call (School School) {
                  Return Observable.from (School.getstudentlist ());  }). map (new func1<school.student, string> () {@Override
                    Public String Call (School.student Student) {return student.getname ();
                    }). Subscribe (New action1<string> () {@Override
                    public void Call (String s) {LOGUTILS.A (s);

    }
                }); /** * Flagmap * If printing the National School name * * private void IniClick11 () {list<school> List = new A
        Rraylist<> ();
            for (int i=0;i<5;i++) {School School = new School ();
            School.setname ("name" +i);
            list<school.student> students = new arraylist<> ();
                for (int y=0;y<=i;y++) {school.student Student = new School.student ();
               Student.setname ("Student name" +i+ "-" +y); Students.add (student);
            } school.setstudentlist (students);
        List.add (school);
                    Observable.from (list). Map (new Func1<school, string> () {@Override
                    Public String Call (School School) {return school.getname ();
                    }). Subscribe (New action1<string> () {@Override
                    public void Call (String s) {LOGUTILS.A (s);

    }
                }); }/** * Map/private void IniClick10 () {observable.just (1234). Map (New Func
                        1<integer, string> () {@Override public String call (integer integer) {
                    integer+1000+ return ""; }). Subscribe (New AcTion1<string> () {@Override public void call (String s) {
                    LOGUTILS.A (s);

    }
                }); /** * Simulate background record Data update to foreground process * to explain briefly, Subscribeon () determines which scheduler the launch data executes on, * Observeon (Androidschedulers.mai
     Nthread ()) specifies that data reception occurs on the UI thread, which is simply not convenient. * * private void IniClick9 () {observable.create (new observable.onsubscribe<string> () {@Over
                Ride public void Call (SUBSCRIBER&LT. Super string> Subscriber) {//Database fetch data
                The String is = ' this is main ';
                try {thread.sleep (3000);
                catch (Interruptedexception e) {e.printstacktrace ();
             }//Launch Subscriber.onnext (IS);
              }). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ())  . Subscribe (New action1<string> () {@Override public void call (String s) {
                    LOGUTILS.A (s);




    }
                }); The OnNext method of/** *mobserver will receive the data from justobservable "Just1", "Just2" in turn, * and if you don't care if the data is received or if there is an error, you do not need observer  OnCompleted () and OnError () methods, * You can use Action1,subscribe () support to pass Action1 as a parameter, and Rxjava will invoke its call method to receive data,/private void IniClick8 () {observable.just ("This action", "This is Action"). Subscribe (New Action1<str Ing> () {@Override public void call (String s) {Logutil
                    S.A (s);
    }
                });
        /** * uses repeat () to create a observable that repeatedly emits specific data: * The following example is repeated 2 times * */private void IniClick7 () {
                    Observable.just ("Wo shi repeat"). Repeat (2). Subscribe (New observer<string> () {@Override public void oncompleted () {LOGUTILS.A ("oncompleted");

                    @Override public void OnError (Throwable e) {} @Override public void OnNext (String s) {LOGUTILS.A ("OnNext

                    : "+s);
    }
                });
 /** * uses timer () to create a observable that emits a special value after a given delay, equivalent to the handler Postdelay () method in Android.

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.