RXjava简单使用
package com.bwie.categorydemo.rxjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.bwie.categorydemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

public class ControlActivity extends AppCompatActivity {
    private static final String TAG = "ControlActivity";
    private List<Student> stus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);
        ButterKnife.bind(this);

        initData();
    }

    private void initData() {
        stus = new ArrayList<>();

        Score s1 = new Score(10, 30, 20);
        List<String> h1 = new ArrayList<>();
        h1.add("aaa");
        h1.add("bbb");
        h1.add("ccc");
        Student stu1 = new Student(1, "zhangsan", s1);
        stu1.setHobby(h1);

        Score s2 = new Score(40, 30, 20);
        List<String> h2 = new ArrayList<>();
        h2.add("ddd");
        h2.add("eee");
        h2.add("fff");
        Student stu2 = new Student(2, "lisi", s2);
        stu2.setHobby(h2);

        Score s3 = new Score(10, 90, 30);
        List<String> h3 = new ArrayList<>();
        h3.add("ggg");
        h3.add("hhh");
        h3.add("iii");
        Student stu3 = new Student(3, "wangwu", s3);
        stu3.setHobby(h3);

        stus.add(stu1);
        stus.add(stu2);
        stus.add(stu3);

    }

    @OnClick({R.id.btn_rx_map, R.id.btn_rx_flatmap})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_rx_map:
                map();
                break;
            case R.id.btn_rx_flatmap:
                flatmap();
                break;
        }
    }

    private void map() {
        Observable.create(new Observable.OnSubscribe<Score>() {
            @Override
            public void call(Subscriber<? super Score> subscriber) {
                subscriber.onNext(new Score(10, 20, 30));
                subscriber.onNext(new Score(20, 30, 40));
                subscriber.onNext(new Score(30, 40, 50));
                subscriber.onNext(new Score(40, 50, 60));
                subscriber.onCompleted();
            }
        }).map(new Func1<Score, Integer>() {
            @Override
            public Integer call(Score score) {
                return score.getChinese() + score.getMath() + score.getEnglish();
            }
        }).map(new Func1<Integer, String>() {
            @Override
            public String call(Integer integer) {
                return "总成绩是" + integer;
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG, s);
            }
        });
    }

    /**
     * 平铺
     */
    private void flatmap() {
        Observable.from(stus)
                .flatMap(new Func1<Student, Observable<String>>() {

                    @Override
                    public Observable<String> call(Student student) {
                        return Observable.from(student.getHobby());
                    }
                })
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        Log.i(TAG, "call: " + s);
                    }
                });
    }
}
阅读更多
想对作者说点什么? 我来说一句

Redis的简单使用初探

-

学院

1970年01月01日 08:00

Rxjava+Retrofit的简单使用Demo

2017年10月20日 2.8MB 下载

RxJava操作符

2016年07月01日 25.51MB 下载

没有更多推荐了,返回首页

不良信息举报

RXjava简单使用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭