RxJava/RxAndroid : buffer(int count)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private String TAG = "输出";
private Observer<List<Integer>> mObserver = new Observer<List<Integer>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<Integer> list) {
Log.d(TAG, "onNext");
for (int i = 0; i < list.size(); i++) {
Log.d(TAG, "onNext:" + list.get(i));
}
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Integer[] numbers = new Integer[]{0, 1, 2, 3, 4};
Observable.fromArray(numbers)
.buffer(2)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(mObserver);
}
}
output:
01-19 17:02:45.736 18276-18276/? D/输出: onNext
01-19 17:02:45.736 18276-18276/? D/输出: onNext:0
01-19 17:02:45.736 18276-18276/? D/输出: onNext:1
01-19 17:02:45.736 18276-18276/? D/输出: onNext
01-19 17:02:45.736 18276-18276/? D/输出: onNext:2
01-19 17:02:45.736 18276-18276/? D/输出: onNext:3
01-19 17:02:45.736 18276-18276/? D/输出: onNext
01-19 17:02:45.736 18276-18276/? D/输出: onNext:4
01-19 17:02:45.736 18276-18276/? D/输出: onComplete