1
- package com .rxjava2 .android .samples .ui .operators ;import android .os .Bundle ;import android .util .Log ;import android .view .View ;import android .widget .Button ;import android .widget .TextView ;import com .rxjava2 .android .samples .R ;import com .rxjava2 .android .samples .utils .AppConstant ;import java .util .concurrent .TimeUnit ;import androidx .appcompat .app .AppCompatActivity ;import io .reactivex .Observable ;import io .reactivex .android .schedulers .AndroidSchedulers ;import io .reactivex .functions .Consumer ;import io .reactivex .schedulers .Schedulers ;public class WindowExampleActivity extends AppCompatActivity { private static final String TAG = WindowExampleActivity .class .getSimpleName (); Button btn ; TextView textView ; @ Override protected void onCreate (Bundle savedInstanceState ) { super .onCreate (savedInstanceState ); setContentView (R .layout .activity_example ); btn = findViewById (R .id .btn ); textView = findViewById (R .id .textView ); btn .setOnClickListener (new View .OnClickListener () { @ Override public void onClick (View view ) { doSomeWork (); } }); } /* * Example using window operator -> It periodically * subdivide items from an Observable into * Observable windows and emit these windows rather than * emitting the items one at a time */ protected void doSomeWork () { Observable .interval (1 , TimeUnit .SECONDS ).take (12 ) .window (3 , TimeUnit .SECONDS ) .subscribeOn (Schedulers .io ()) .observeOn (AndroidSchedulers .mainThread ()) .subscribe (getConsumer ()); } public Consumer <Observable <Long >> getConsumer () { return new Consumer <Observable <Long >>() { @ Override public void accept (Observable <Long > observable ) { Log .d (TAG , "Sub Divide begin...." ); textView .append ("Sub Divide begin ...." ); textView .append (AppConstant .LINE_SEPARATOR ); observable .subscribeOn (Schedulers .io ()) .observeOn (AndroidSchedulers .mainThread ()) .subscribe (new Consumer <Long >() { @ Override public void accept (Long value ) { Log .d (TAG , "Next:" + value ); textView .append ("Next:" + value ); textView .append (AppConstant .LINE_SEPARATOR ); } }); } }; } }
1
+ package com .rxjava2 .android .samples .ui .operators ;
2
+
3
+ import android .os .Bundle ;
4
+ import android .util .Log ;
5
+ import android .view .View ;
6
+ import android .widget .Button ;
7
+ import android .widget .TextView ;
8
+
9
+ import com .rxjava2 .android .samples .R ;
10
+ import com .rxjava2 .android .samples .utils .AppConstant ;
11
+
12
+ import java .util .concurrent .TimeUnit ;
13
+
14
+ import androidx .appcompat .app .AppCompatActivity ;
15
+ import io .reactivex .Observable ;
16
+ import io .reactivex .android .schedulers .AndroidSchedulers ;
17
+ import io .reactivex .functions .Consumer ;
18
+ import io .reactivex .schedulers .Schedulers ;
19
+
20
+ public class WindowExampleActivity extends AppCompatActivity {
21
+
22
+ private static final String TAG = WindowExampleActivity .class .getSimpleName ();
23
+ Button btn ;
24
+ TextView textView ;
25
+
26
+ @ Override
27
+ protected void onCreate (Bundle savedInstanceState ) {
28
+ super .onCreate (savedInstanceState );
29
+ setContentView (R .layout .activity_example );
30
+ btn = findViewById (R .id .btn );
31
+ textView = findViewById (R .id .textView );
32
+
33
+ btn .setOnClickListener (new View .OnClickListener () {
34
+ @ Override
35
+ public void onClick (View view ) {
36
+ doSomeWork ();
37
+ }
38
+ });
39
+ }
40
+
41
+ /*
42
+ * Example using window operator -> It periodically
43
+ * subdivide items from an Observable into
44
+ * Observable windows and emit these windows rather than
45
+ * emitting the items one at a time
46
+ */
47
+ protected void doSomeWork () {
48
+
49
+ Observable .interval (1 , TimeUnit .SECONDS ).take (12 )
50
+ .window (3 , TimeUnit .SECONDS )
51
+ .subscribeOn (Schedulers .io ())
52
+ .observeOn (AndroidSchedulers .mainThread ())
53
+ .subscribe (getConsumer ());
54
+ }
55
+
56
+ public Consumer <Observable <Long >> getConsumer () {
57
+ return new Consumer <Observable <Long >>() {
58
+ @ Override
59
+ public void accept (Observable <Long > observable ) {
60
+ Log .d (TAG , "Sub Divide begin...." );
61
+ textView .append ("Sub Divide begin ...." );
62
+ textView .append (AppConstant .LINE_SEPARATOR );
63
+ observable
64
+ .subscribeOn (Schedulers .io ())
65
+ .observeOn (AndroidSchedulers .mainThread ())
66
+ .subscribe (new Consumer <Long >() {
67
+ @ Override
68
+ public void accept (Long value ) {
69
+ Log .d (TAG , "Next:" + value );
70
+ textView .append ("Next:" + value );
71
+ textView .append (AppConstant .LINE_SEPARATOR );
72
+ }
73
+ });
74
+ }
75
+ };
76
+ }
77
+ }
0 commit comments