Android响应式编程开发RxAndroid(2):简单例程的Android改造
附录1的Android响应式编程开发代码规范,遵循的是RxJava样式,可以正常运行。换一种方式,把响应式编程写的更Android化,下面对附录1的例程改造,换一种方式实现。用一个TextView显示最终的处理结果(在onComplete里面处理结果)。
Java代码:
package zhangphil.app;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends Activity {
//假设一个数据源
private String TAG = "zhangphil@csdn";
private List<String> lists = new ArrayList<>();
private DisposableObserver<List<String>> mDisposableObserver = new DisposableObserver<List<String>>() {
private String string = "";
@Override
public void onNext(List list) {
Log.d(TAG, "onNext - Start");
for (int i = 0; i < list.size(); i++) {
String s = String.valueOf(list.get(i));
string = string + s;
Log.d(TAG, string);
SystemClock.sleep(100);
}
Log.d(TAG, "onNext - End");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
text.setText(string);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
for (int i = 0; i < TAG.length(); i++) {
lists.add(String.valueOf(TAG.charAt(i)));
}
//just作为输入驱动数据源
Observable.just(lists)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(mDisposableObserver);
Log.d(TAG,"onCreate结束");
}
}
代码运行输出:
02-28 15:04:53.135 25048-25048/zhangphil.app D/zhangphil@csdn: onCreate结束
02-28 15:04:53.135 25048-25048/zhangphil.app D/Activity: performCreate Call Injection manager
02-28 15:04:53.145 25048-25048/zhangphil.app I/InjectionManager: dispatchOnViewCreated > Target : zhangphil.app.MainActivity isFragment :false
02-28 15:04:53.145 25048-25048/zhangphil.app D/SecWifiDisplayUtil: Metadata value : SecSettings2
02-28 15:04:53.145 25048-25048/zhangphil.app D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{591deef I.E...... R.....ID 0,0-0,0}
02-28 15:04:53.145 25048-25048/zhangphil.app D/zhangphil@csdn: onNext - Start
02-28 15:04:53.145 25048-25048/zhangphil.app D/zhangphil@csdn: z
02-28 15:04:53.245 25048-25048/zhangphil.app D/zhangphil@csdn: zh
02-28 15:04:53.345 25048-25048/zhangphil.app D/zhangphil@csdn: zha
02-28 15:04:53.445 25048-25048/zhangphil.app D/zhangphil@csdn: zhan
02-28 15:04:53.545 25048-25048/zhangphil.app D/zhangphil@csdn: zhang
02-28 15:04:53.645 25048-25048/zhangphil.app D/zhangphil@csdn: zhangp
02-28 15:04:53.745 25048-25048/zhangphil.app D/zhangphil@csdn: zhangph
02-28 15:04:53.855 25048-25048/zhangphil.app D/zhangphil@csdn: zhangphi
02-28 15:04:53.955 25048-25048/zhangphil.app D/zhangphil@csdn: zhangphil
02-28 15:04:54.045 25048-25048/zhangphil.app D/zhangphil@csdn: zhangphil@
02-28 15:04:54.145 25048-25048/zhangphil.app D/zhangphil@csdn: zhangphil@c
02-28 15:04:54.245 25048-25048/zhangphil.app D/zhangphil@csdn: zhangphil@cs
02-28 15:04:54.345 25048-25048/zhangphil.app D/zhangphil@csdn: zhangphil@csd
02-28 15:04:54.455 25048-25048/zhangphil.app D/zhangphil@csdn: zhangphil@csdn
02-28 15:04:54.555 25048-25048/zhangphil.app D/zhangphil@csdn: onNext - End
02-28 15:04:54.555 25048-25048/zhangphil.app D/zhangphil@csdn: onComplete
当在onNext里面的遍历结束后,在TextView里面才显示zhangphil@csdn。
附录:
1,《Android响应式编程RxAndroid(1):简单例程》链接地址:http://blog.csdn.net/zhangphil/article/details/58593782