天天看點

RxAndroid、RxJava的fromCallable更新資料加載到ListView簡例



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>
           

代碼運作結果:

緻謝:

馮祖學對本文有重大貢獻!

繼續閱讀