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);
                    }
                });
    }
}

猜你喜欢

转载自blog.csdn.net/l6666_6666/article/details/79948993