安卓中Rxjava

RxJava的核心原理其实就是典型的响应者模式,这并不难,但是它很巧妙的把这种响应者模式进行了链式的连接,这应该就是一种响应式编程的方式了吧。我把整个学习过程分成了三步,一步一步解密RxJava(代码尽量贴近RxJava源码)。同时为了练习Kotlin,因此决定用Kotlin来进行书写。

Observable 即被观察者,它决定什么时候触发事件以及触发怎样的事件。 RxJava 使用 create() 方法来创建一个 Observable ,并为它定义事件触发规则

 public class Rxjava {
        public static void main(String[] args) {
           /* new Observable(){
    
                @Override
            protected void subscribeActual(Observer observer) {


                observer.onNext("玻璃瓶");
                observer.onNext("啤酒瓶");
                observer.onComplete();
            }
        }.subscribe(new Observer() {
            @Override
            public void onSubscribe(Disposable d) {


            }

            @Override
            public void onNext(String s) {

                System.out.println("接收到了:" + s);

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                System.out.println("接收数据完毕");

            }
        });*/

//        mapOpretor();
//        flatMapOpretor();
//        groupByOperator();
//        filterOperator();
//        distinctOperator();
//        ignoreElements();
//        elementAtOperator();
        windowOperator();
    }

    //map操作符
    public static  void mapOpretor(){
        Observable.just("HELLO")
         .map(new Function() {
            @Override
            public String apply(String s) throws Exception {
                return s.toLowerCase();

            }
        })
                .map(new Function() {
                    @Override
                    public String apply(String s) throws Exception {
                        return s + ",world";
                    }
                })
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        System.out.println("接收到的数据是:" + s);


                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    //flatMap操作符
    public static void flatMapOpretor() {

        User user = new User();
        user.userName = "zhangsan";
        user.addresss = new ArrayList<>();
        User.Address address1 = new User.Address();
//        User.Address address1 = new User.Address();
        address1.street = "上地九街";
        address1.city = "北京";
        user.addresss.add(address1);

        User.Address address2 = new User.Address();
        address2.street = "上地十街";
        address2.city = "上海";
        user.addresss.add(address2);

        /*Observable.just(user)
                .map(new Function>() {
                    @Override
                    public List apply(User user) throws Exception {
                        return user.addresss;
                    }
                }).subscribe(new Consumer>() {
            @Override
            public void accept(List addresses) throws Exception {
                for (int i = 0; i < addresses.size(); i++) {
                    System.out.println("地址是:" + addresses.get(i).street);
                }
            }
        });*/

        Observable.just(user)
                .flatMap(new Function>() {
                    @Override
                    public ObservableSource apply(User user) throws Exception {
                        return Observable.fromIterable(user.addresss);
                    }
                })

                .subscribe(new Consumer() {
            @Override
            public void accept(User.Address address) throws Exception {
                System.out.println("地址是:" + address.street);

            }
        });

    }

    //groupBy操作符
    public static void groupByOperator(){
        Observable
                .range(1,8)
                .groupBy(new Function() {
                    @Override
                    public String apply(Integer integer) throws Exception {
                        return (integer % 2 == 0)? "偶数组":"奇数组";
                    }
                }).subscribe(new Consumer>() {
            @Override
            public void accept(final GroupedObservable stringIntegerGroupedObservable) throws Exception {
                if(stringIntegerGroupedObservable.getKey().equals("奇数组")){
                    stringIntegerGroupedObservable.subscribe(new Consumer() {
                        @Override
                        public void accept(Integer integer) throws Exception {
                            System.out.println(stringIntegerGroupedObservable.getKey() + "member:"  + integer);
                        }
                    });
                }/*else if(stringIntegerGroupedObservable.getKey().equals("偶偶组")){
                    stringIntegerGroupedObservable.subscribe(new Consumer() {
                        @Override
                        public void accept(Integer integer) throws Exception {
                            System.out.println(stringIntegerGroupedObservable.getKey() + "member:"  + integer);

                        }
                    });
                }*/

                System.out.println();
            }
        });
    }

    //filter操作符
    public static void filterOperator(){

        Observable.just(1,8,20,3,50,4,6,100)
                .filter(new Predicate() {
                    @Override
                    public boolean test(Integer integer) throws Exception {
                        return integer > 8;
                    }
                }).subscribe(new Consumer() {
            @Override
            public void accept(Integer integer) throws Exception {
                System.out.println("next :" + integer);
            }
        });

    }

    //distinct过滤掉重复的数据项
    public static void distinctOperator() {
        Observable.just("A","A","B")
                .distinct()
                .subscribe(new Consumer() {
                    @Override
                    public void accept(String integer) throws Exception {
                        System.out.println("next :" + integer);

                    }
                });
    }

    //ignoreElements 不发射任何数据
    public static void ignoreElements() {
        Observable.just(1,2,3,4,5)
                .ignoreElements()
                .subscribe(new Action() {
                    @Override
                    public void run() throws Exception {
                        System.out.println("完成了任务");

                    }
                });
    }


    //elementAt 只发射第n项数据
    public static void elementAtOperator() {
        Observable.just(1,2,3,4,5,6,7)
                .elementAt(2)
//                .elementAt(12,34)
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        System.out.println("next:" + integer);

                    }
                });
    }

    //window操作符
    public static void windowOperator() {
        Observable.range(1,10)
                .window(2)
                .subscribe(new Consumer>() {
                    @Override
                    public void accept(Observable integerObservable) throws Exception {
                        System.out.println("On next:" );
                        integerObservable.subscribe(new Consumer() {
                            @Override
                            public void accept(Integer integer) throws Exception {
                                System.out.println("accept:" + integer );

                            }
                        });

                    }
                });
    }


}

你可能感兴趣的:(安卓中Rxjava)