25
25
26
26
package com .malin .rxjava .activity ;
27
27
28
+ import android .content .Context ;
28
29
import android .graphics .Bitmap ;
29
30
import android .graphics .Canvas ;
30
31
import android .graphics .Paint ;
31
32
import android .graphics .PorterDuff ;
32
33
import android .graphics .PorterDuffXfermode ;
33
34
import android .graphics .drawable .Drawable ;
34
35
import android .os .Bundle ;
36
+ import android .support .v4 .content .ContextCompat ;
35
37
import android .support .v7 .app .AppCompatActivity ;
36
38
import android .text .TextUtils ;
37
39
import android .util .Log ;
50
52
import com .jakewharton .rxbinding .widget .RxTextView ;
51
53
import com .jakewharton .rxbinding .widget .TextViewTextChangeEvent ;
52
54
import com .malin .rxjava .R ;
55
+ import com .malin .rxjava .application .RxJavaApplication ;
53
56
import com .malin .rxjava .constant .Constant ;
54
57
import com .malin .rxjava .factory .DataFactory ;
55
58
import com .malin .rxjava .factory .ImageNameFactory ;
@@ -109,18 +112,18 @@ public class MainActivity extends AppCompatActivity {
109
112
private ProgressBar mProgressBar ;
110
113
private EditText mSearchEditText ;
111
114
private TextView mResultTextView ;
115
+ private Context mContext ;
112
116
113
117
@ Override
114
118
protected void onCreate (Bundle savedInstanceState ) {
115
119
super .onCreate (savedInstanceState );
116
120
setContentView (R .layout .activity_main );
117
121
initializeLogAndDeviceInfo ();
118
122
initView ();
119
- testFuncation (0 );//RxJava基础概念的练习
123
+ initData ();
124
+ testFuncation (9 );//RxJava基础概念的练习
120
125
}
121
126
122
-
123
-
124
127
/**
125
128
* 初始化Logger日志输出配置和获取手机尺寸信息
126
129
*/
@@ -133,12 +136,16 @@ private void initializeLogAndDeviceInfo() {
133
136
* 用于显示图片的初始化
134
137
*/
135
138
private void initView () {
139
+ mContext = this ;
136
140
mImageView = (ImageView ) findViewById (R .id .iv_image );
137
141
mResultTextView = (TextView ) findViewById (R .id .tv_result );
138
142
mSearchEditText = (EditText ) findViewById (R .id .ed_search );
139
143
mProgressBar = (ProgressBar ) findViewById (R .id .progressbar );
140
144
}
141
145
146
+ private void initData () {
147
+ mContext = this ;
148
+ }
142
149
143
150
/**
144
151
* 故意让程序出现异常,可以用来测试
@@ -244,7 +251,7 @@ public void onError(Throwable e) {
244
251
@ Override
245
252
public void onNext (String s ) {
246
253
Logger .d ("观察者-observer:onNext():" + s );
247
- // getException();//故意让程序出现异常,用于测试onError()方法的执行....
254
+ //getException();//故意让程序出现异常,用于测试onError()方法的执行....
248
255
}
249
256
};
250
257
@@ -294,7 +301,7 @@ public void onError(Throwable e) {
294
301
public void onNext (Object o ) {
295
302
String str = (String ) o ;
296
303
Logger .d ("观察者-observer:onNext():" + str );
297
- // getException();//故意让程序出现异常,用于测试onError()方法的执行....
304
+ // getException();//故意让程序出现异常,用于测试onError()方法的执行....
298
305
}
299
306
};
300
307
@@ -472,13 +479,13 @@ private void method6() {
472
479
@ Override
473
480
public void call (Subscriber <? super Drawable > subscriber ) {
474
481
Logger .d ("被观察者" );
475
- Drawable drawable = getResources () .getDrawable (drawableRes );
482
+ Drawable drawable = ContextCompat .getDrawable (RxJavaApplication . getApplication (), drawableRes );
476
483
subscriber .onNext (drawable );
477
484
subscriber .onCompleted ();
478
485
}
479
486
})
480
487
.subscribeOn (Schedulers .io ())//事件产生的线程。指定 subscribe() 发生在 IO 线程
481
- // doOnSubscribe() 之后有 subscribeOn () 的话,它将执行在离它最近的 subscribeOn () 所指定的线程。这里将执行在主线程中
488
+ // doOnSubscribe() 之后有 observeOn () 的话,它将执行在离它最近的 observeOn () 所指定的线程。这里将执行在主线程中
482
489
.doOnSubscribe (new Action0 () {
483
490
@ Override
484
491
public void call () {
@@ -491,7 +498,7 @@ public void call() {
491
498
.subscribe (new Subscriber <Drawable >() { //3:订阅 //2:观察者
492
499
@ Override
493
500
public void onCompleted () {
494
- if (mProgressBar != null ){
501
+ if (mProgressBar != null ) {
495
502
mProgressBar .setVisibility (View .GONE );
496
503
}
497
504
Logger .d ("观察者 onCompleted()" );
@@ -500,7 +507,7 @@ public void onCompleted() {
500
507
501
508
@ Override
502
509
public void onError (Throwable e ) {
503
- if (mProgressBar != null ){
510
+ if (mProgressBar != null ) {
504
511
mProgressBar .setVisibility (View .GONE );
505
512
}
506
513
Logger .d ("观察者 onError()" );
@@ -512,6 +519,7 @@ public void onError(Throwable e) {
512
519
public void onNext (Drawable drawable ) {
513
520
Toast .makeText (MainActivity .this , "观察者 onNext()" , Toast .LENGTH_SHORT ).show ();
514
521
Logger .d ("观察者 onNext()" );
522
+ if (mImageView == null || drawable == null ) return ;
515
523
mImageView .setImageDrawable (drawable );
516
524
}
517
525
});
@@ -529,11 +537,11 @@ private void method7() {
529
537
@ Override
530
538
public Drawable call (Integer integer ) {// 参数类型 String
531
539
Logger .d ("integer:" + integer );
532
- return getResources () .getDrawable (integer );
540
+ return ContextCompat .getDrawable (RxJavaApplication . getApplication (), integer );
533
541
}
534
542
})
535
543
.subscribeOn (Schedulers .io ())//事件产生的线程。指定 subscribe() 发生在 IO 线程
536
- //doOnSubscribe() 之后有 subscribeOn () 的话,它将执行在离它最近的 subscribeOn () 所指定的线程。这里将执行在主线程中
544
+ //doOnSubscribe() 之后有 observeOn () 的话,它将执行在离它最近的 observeOn () 所指定的线程。这里将执行在主线程中
537
545
.doOnSubscribe (new Action0 () {
538
546
@ Override
539
547
public void call () {
@@ -563,6 +571,7 @@ public void onError(Throwable e) {
563
571
564
572
@ Override
565
573
public void onNext (Drawable drawable ) {
574
+ if (mImageView == null || drawable == null ) return ;
566
575
mImageView .setImageDrawable (drawable );
567
576
Logger .d ("观察者:onNext(Drawable drawable):" + drawable .toString ());
568
577
}
@@ -591,7 +600,7 @@ private void method8() {
591
600
* {@link #method8()}
592
601
*/
593
602
private void method9 () {
594
- //just(T...): 将传入的参数依次发送出来,实现遍历的目的
603
+ //just(T...): 将传入的参数依次发送出来,实现遍历的目的
595
604
Observable .from (DataFactory .getData ())
596
605
.subscribeOn (Schedulers .io ())
597
606
.observeOn (AndroidSchedulers .mainThread ())
@@ -605,7 +614,7 @@ public void call(Student student) {
605
614
606
615
607
616
/**
608
- * 需要:输出学生的姓名:将每个学生的(姓名)依次发射出去
617
+ * 需要:输出学生的姓名:将每个学生的(姓名)依次发射出去
609
618
* RxJava解决方案
610
619
* {@link #method9()}
611
620
*/
@@ -709,7 +718,7 @@ public void onError(Throwable e) {
709
718
public void onNext (Student student ) {
710
719
ArrayList <Course > courses = student .courses ;
711
720
for (Course course : courses ) {
712
- Logger .d ("观察者:" +course .name );
721
+ Logger .d ("观察者:" + course .name );
713
722
}
714
723
}
715
724
});
@@ -744,6 +753,7 @@ public void call(ArrayList<Course> courses) {
744
753
}
745
754
746
755
//---------------------------------------10: flatMap()的使用-------------------------------------------------------------
756
+
747
757
/**
748
758
* 需要:输出每一个学生选修的课程,对method13的简化
749
759
* 嵌套循环的RxJava解决方案
@@ -782,14 +792,13 @@ public void call(Course course) {
782
792
}
783
793
784
794
785
- //---------------------------------------10: RxBinding的引入-------------------------------------------------------------
795
+ //---------------------------------------10: RxBinding的引入-------------------------------------------------------------
786
796
787
797
788
798
/**
789
799
* 需要防止快速连续点击,短时间内连续点击.
790
- *
791
800
*/
792
- private void method15 (){
801
+ private void method15 () {
793
802
794
803
795
804
mImageView .setOnClickListener (new View .OnClickListener () {
@@ -809,8 +818,8 @@ public void run() {
809
818
});
810
819
811
820
812
-
813
821
}
822
+
814
823
/**
815
824
* RxBinding
816
825
* RxBinding 是 Jake Wharton 的一个开源库,它提供了一套在 Android 平台上的基于 RxJava 的 Binding API。
@@ -848,7 +857,7 @@ public void call(Void aVoid) {
848
857
/**
849
858
* EditText,每隔500ms,去响应变化
850
859
*/
851
- private void method18 (){
860
+ private void method18 () {
852
861
mSearchEditText .setVisibility (View .VISIBLE );
853
862
RxTextView .textChangeEvents (mSearchEditText )
854
863
.debounce (500 , TimeUnit .MILLISECONDS )
@@ -866,8 +875,8 @@ public void onError(Throwable e) {
866
875
public void onNext (TextViewTextChangeEvent textViewTextChangeEvent ) {
867
876
String changedMessage = textViewTextChangeEvent .text ().toString ();
868
877
Logger .d (TAG , changedMessage );
869
- if (!TextUtils .isEmpty (changedMessage )){
870
- ToastUtil .getInstance ().showToast (MainActivity .this ,changedMessage );
878
+ if (!TextUtils .isEmpty (changedMessage )) {
879
+ ToastUtil .getInstance ().showToast (MainActivity .this , changedMessage );
871
880
}
872
881
}
873
882
});
@@ -879,7 +888,6 @@ public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
879
888
//操作符号 Range操作符根据出入的初始值n和数目m发射一系列大于等于n的m个值
880
889
//例如:实现:输出1,2,3,4,5
881
890
// 其使用也非常方便,仅仅制定初始值和数目就可以了,不用自己去实现对Subscriber的调用
882
-
883
891
private void method19 () {
884
892
Observable .range (1 , 5 )
885
893
.subscribeOn (Schedulers .io ())
@@ -944,7 +952,7 @@ public void onFailure(Call<User> call, Throwable t) {
944
952
/**
945
953
* 使用Retrofit网络库,同时使用RxJava 获取androidmalin的GitHub个人信息
946
954
*/
947
- private void method22 (){
955
+ private void method22 () {
948
956
//TODO:1:被观察者,数据源
949
957
//TODO:2:观察者
950
958
//TODO:3:订阅,被观察者 被 观察者订阅
@@ -977,7 +985,7 @@ public void onError(Throwable e) {
977
985
978
986
@ Override
979
987
public void onNext (User user ) {
980
- Logger .d ( "onNext()" );
988
+ Logger .d ("onNext()" );
981
989
String message = "Github Name :" + user .name + "\n Website :" + user .blog + "\n Company Name :" + user .company ;
982
990
Toast .makeText (MainActivity .this , "onNext" , Toast .LENGTH_SHORT ).show ();
983
991
Logger .d (message );
@@ -987,7 +995,6 @@ public void onNext(User user) {
987
995
}
988
996
989
997
990
-
991
998
private ArrayAdapter <String > mAdapter ;
992
999
private CompositeSubscription mSubscription = new CompositeSubscription ();
993
1000
private ListView mResultListView ;
@@ -1050,7 +1057,6 @@ public void onNext(List<Contributor> contributors) {
1050
1057
}
1051
1058
1052
1059
1053
-
1054
1060
private static final int COUNT = 10 ;
1055
1061
private static final int TIME_ALL = 5000 ;
1056
1062
private ArrayList <Long > timeList = new ArrayList <>();
@@ -1219,28 +1225,28 @@ private void testFuncation(int number) {
1219
1225
}
1220
1226
1221
1227
1222
- case 19 :{
1228
+ case 19 : {
1223
1229
method19 ();
1224
1230
break ;
1225
1231
}
1226
1232
1227
- case 20 :{
1233
+ case 20 : {
1228
1234
method20 ();
1229
1235
break ;
1230
1236
}
1231
1237
1232
- case 21 :{
1238
+ case 21 : {
1233
1239
method21 ();
1234
1240
break ;
1235
1241
}
1236
1242
1237
1243
1238
- case 22 :{
1244
+ case 22 : {
1239
1245
method22 ();
1240
1246
break ;
1241
1247
}
1242
1248
1243
- case 23 :{
1249
+ case 23 : {
1244
1250
method23 ();
1245
1251
break ;
1246
1252
}
@@ -1307,8 +1313,8 @@ private void recycleImageView() {
1307
1313
* 递归释放所有子view涉及的图片,背景,DrawingCache,监听器等等资源,
1308
1314
* 让Activity成为一个不占资源的空壳,泄露了也不会导致图片资源被持有。
1309
1315
*
1310
- * @description Unbind the rootView
1311
1316
* @param view:the root view of the layout
1317
+ * @description Unbind the rootView
1312
1318
1313
1319
* @link http://stackoverflow.com/questions/9461364/exception-in-unbinddrawables
1314
1320
* http://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=400656149&idx=1&sn=122b4f4965fafebf78ec0b4fce2ef62a&3rd=MzA3MDU4NTYzMw==&scene=6#rd
0 commit comments