rxjava 常用用法執行個體

來源:互聯網
上載者:User

具體怎麼用我就不說了 你們都懂 我直接上傳案例吧

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 savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_rxjava);        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                iniClick17();            }        });    }    /**     * 過濾filter     */    private void iniClick17() {        Observable.just(1,2,3,4,5,6)                .filter(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:去掉重複的項,比較好理解:     */    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(serializable);                    }                });    }    /**     * Take:發射前n項資料,還是用上面的例子,假設不要改所有學校的名稱了,就改前四個學校的名稱:     */    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("嘻哈哈大學");                        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 和 buffer 結合使用     */    private void iniClick14() {        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)                .map(new Func1<School, School>() {                    @Override                    public School call(School school) {                        school.setName("nb大學");                        return school;                    }                })                .buffer(list.size())                .subscribe(new Action1<List<School>>() {                    @Override                    public void call(List<School> schools) {                    }                });    }    /**     * Buffer:緩衝,可以設定緩衝大小,緩衝滿後,以list的方式將資料發送出去;例:     */    private void iniClick13() {        Observable.just(1,2,3,4,5)                .buffer(2)                .subscribe(new Action1<List<Integer>>() {                    @Override                    public void call(List<Integer> integers) {                        LogUtils.a(integers.size());                    }                });    }    /**     * flagmap     * 若列印全國的學生的名字     * 總之 flagmap 就是吧一個observer轉化為另一個observer     */    private void iniClick12() {        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     * 若列印全國的學校名稱     */    private void iniClick11() {        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)                .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 Func1<Integer, String>() {                    @Override                    public String call(Integer integer) {                        //2000                        return integer+1000+"";                    }                })                .subscribe(new Action1<String>() {                    @Override                    public void call(String s) {                        LogUtils.a(s);                    }                });    }    /**     * 類比後台 記載資料更新到前台進程     * 簡單粗暴的解釋一下,subscribeOn( )決定了發射資料在哪個調度器上執行,     * observeOn(AndroidSchedulers.mainThread())則指定資料接收發生在UI線程,簡直不要太方便。     */    private void iniClick9() {        Observable.create(new Observable.OnSubscribe<String>() {            @Override            public void call(Subscriber<? super String> subscriber) {                //資料庫擷取資料                String is = "this is main";                try {                    Thread.sleep(3000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                //發射                subscriber.onNext(is);             }        })                .subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Action1<String>() {                    @Override                    public void call(String s) {                        LogUtils.a(s);                    }                });    }    /**     *mObserver的onNext方法將會依次收到來自justObservable的資料"just1"、"just2",     * 另外,如果你不在意資料是否接收完或者是否出現錯誤,即不需要Observer的onCompleted()和onError()方法,     * 可使用Action1,subscribe()支援將Action1作為參數傳入,RxJava將會調用它的call方法來接收資料,     */    private void iniClick8() {        Observable.just("this action 01","this is action 02")                .subscribe(new Action1<String>() {                    @Override                    public void call(String s) {                        LogUtils.a(s);                    }                });    }    /**     * 使用repeat( ),建立一個重複發射特定資料的Observable:     * 下例是重複發送2次     *     */    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);                    }                });    }    /**     * 使用timer( ),建立一個Observable,它在一個給定的延遲後發射一個特殊的值,等同於Android中Handler的postDelay( )方法     */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.