RxJava & RxAndroid 简单入门学习

移动开发 AndroidRxJava
Zyao89 2017年3月12日星期日 23:51

由于时间问题,只是先入个门,用一用,后面还会做进一步的学习。

# 引入Gradle

compile 'io.reactivex.rxjava2:rxjava:2.0.6'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
1
2

# 代码

package com.zyao89.demorxjava;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity
{
    private Handler mHandler = new Handler();
    private Observable<String> mObservable;
    private Observer<String>   mObserver;
    private TextView           mLogCat;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLogCat = (TextView) findViewById(R.id.logcat);
    }

    /**
     * 创建发送器对象
     * @param view
     */
    public void createObservable(View view)
    {
        printLogCat("createObservable... ");
        mObservable = Observable.create(new ObservableOnSubscribe<String>()
        {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception
            {
                printLogCat("subscribe... e.onNext()...");
                e.onNext("Zyao89");
                e.onComplete();
                printLogCat("subscribe... e.onComplete()...");
            }
        });
    }

    /**
     * 创建接受者对象
     * @param view
     */
    public void createObserver(View view)
    {
        printLogCat("createObserver... ");
        mObserver = new Observer<String>()
        {
            @Override
            public void onSubscribe(Disposable d)
            {
                printLogCat("onSubscribe... ");
            }

            @Override
            public void onNext(String s)
            {
                printLogCat("onNext... s: " + s);
            }

            @Override
            public void onError(Throwable e)
            {
                printLogCat("onError... " + e.getMessage());
            }

            @Override
            public void onComplete()
            {
                printLogCat("onComplete...");
            }
        };
    }

    public void subscribe(View view)
    {
        if (mObservable != null && mObserver != null)
        {
            printLogCat("subscribe...  mObservable.subscribe(mObserver)");
            mObservable.subscribe(mObserver);
        }
    }

    private void printLogCat(Object... texts)
    {
        for (Object text : texts)
        {
            mLogCat.append(text + "\n");
        }
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                ScrollView scrollView = (ScrollView) mLogCat.getParent();
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);//滚动到底部
            }
        });
    }

    public void clear(View view)
    {
        mLogCat.setText("");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

# Github源码

zyao89/DemoRxJava


作者:Zyao89;转载请保留此行,谢谢;

作者: Zyao89; 转载请保留
版权声明: 自由转载-非商用-非衍生-保持署名
上次编辑时间: 2020年8月7日星期五 16:07