RxAndroid、RxJava的fromCallable更新数据加载到ListView简例
Java代码:
package zhangphil.app;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import com.lzy.okgo.OkGo;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.Callable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Response;
/**
*这个例子假设有一个应用场景:每点击一次按钮,然后就启动一个线程加载一个网络图片到ListView
*
* 本例的数据流动:button启动addItem,然后在addItem里面的fromCallable加载回来一个Bitmap。
* 此时fromCallable返回的Bitmap经由RxAndroid机制自动链式丢给DisposableObserver的onNext
*
*/
public class MainActivity extends Activity {
private final String TAG = String.valueOf(UUID.randomUUID());
private ArrayList items = new ArrayList<>();
private ItemAdapter mAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
mAdapter = new ItemAdapter(this, R.layout.item);
listView.setAdapter(mAdapter);
final String url = "http://avatar.csdn.net/9/7/A/1_zhangphil.jpg";
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击按钮增加一个ListView的item
addItem(url);
}
});
// PublishSubject mEmitter = PublishSubject.create();
// mEmitter.map(new Function<Object, Bitmap>() {
// @Override
// public Bitmap apply(Object o) throws Exception {
// String url= (String) o;
// Bitmap bmp=loadingBitmap(url);
// return bmp;
// }
// })
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(mObserver);
}
//假设这里每次都是新启一个耗时操作
private void addItem(final String url) {
Observable.fromCallable(new Callable<Bitmap>() {
@Override
public Bitmap call() throws Exception {
Bitmap bmp = null;
//同步方法返回观察者需要的数据结果
//在这里处理线程化的操作
Response response = OkGo.get(url).tag(TAG).execute();
try {
if (response.isSuccessful()) {
byte[] bytes = response.body().bytes();
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<Bitmap>() {
@Override
public void onNext(Bitmap io) {
items.add(io);
}
@Override
public void onComplete() {
mAdapter.notifyDataSetChanged();
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
});
}
/**
* 以下是常规的Android ListView数据添加和更新Adapter
*
*/
private class ItemAdapter extends ArrayAdapter {
private LayoutInflater inflater;
private int resId;
public ItemAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
inflater = LayoutInflater.from(context);
resId = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(resId, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
imageView.setImageBitmap(getItem(position));
return convertView;
}
@Nullable
@Override
public Bitmap getItem(int position) {
Bitmap bmp = (Bitmap) items.get(position);
return bmp;
}
@Override
public int getCount() {
return items.size();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
OkGo.getInstance().cancelTag(TAG);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
代码运行结果:
致谢:
冯祖学对本文有重大贡献!