1. 程式人生 > >專案rxjava+rxandroid學習

專案rxjava+rxandroid學習

1、寫法1

/**
         * 列印helloworld,使用被觀察者和訂閱者
         * 被觀察者發出事件資訊
         * 訂閱者處理事件
         */
        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello world"
); subscriber.onCompleted(); } }); Subscriber<String> subscriber = new Subscriber<String>() { @Override public void onCompleted() { Log.i("subscriber", "complete"); } @Override public
void onError(Throwable e) { } @Override public void onNext(String s) { Log.i("subscriber", s); } }; observable.subscribe(subscriber);

2、寫法2

    /**
         * 使用map來進行物件的轉化,個人理解map的用法相當於介面卡,將輸入的結果轉化成想要得到的結果
         */
Observable.just("hello").map(new Func1<String, String>() { @Override public String call(String s) { return s+" wrold"; } }).subscribe(new Action1<String>() { @Override public void call(String s) { Log.i("test",s); } });

3、寫法3

Pet.java
public class Pet {
private String Petname;

public Pet(String petname) {
    Petname = petname;
}

public String getPetname() {
    return Petname;
}

public void setPetname(String petname) {
    Petname = petname;
}
}
Master.java
public class Master {
private String name;
private List<Pet> petList=new ArrayList<Pet>();

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<Pet> getPetList() {
    return petList;
}

public void setPetList(List<Pet> petList) {
    this.petList = petList;
}
}

/**
     * 使用flatmap來進行一對多的物件轉化
     */

    List<Master> masterList=new ArrayList<Master>();
    for(int i=0;i<3;i++)
    {
        Master master=new Master();
        master.setName("master"+i);
        for(int j=0;j<3;j++)
        {
            master.getPetList().add(new Pet("pet"+j));
        }
        masterList.add(master);
    }
    for (Master item:
         masterList) {
        Log.i("item",item.getName());
    }

    Observable.from(masterList).flatMap(new Func1<Master, Observable<Pet>>() {
        @Override
        public Observable<Pet> call(Master master) {
            return Observable.from(master.getPetList());
        }
    }).subscribe(new Subscriber<Pet>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(Pet pet) {
            Log.i("PetName",pet.getPetname());
        }
    });

自己寫的demo:

package com.lcwr.android.modules.home.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lcwr.android.R;
import com.xitaiinfo.library.commons.rx.Rx;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * author: lihui
 * version: 1.0
 * date: on 2017/8/4
 * time:on 16:37
 * 描述:
 */

public class Testactivity extends Activity {
    Unbinder unbinder;
    @BindView(R.id.tv)
    TextView mTv;
    @BindView(R.id.button)
    Button bt;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);
        unbinder = ButterKnife.bind(this);
        List<Master> masterList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Master master = new Master("master" + i);
            for (int j = 0; j < 3; j++) {//每個人3個寵物
                Pet pet = new Pet(master.getMastername() + "-pet" + j);
                master.getPetList().add(pet);
            }
            masterList.add(master);//5個人
        }
        for (Master m : masterList) {
            Log.d("lihui", "master m---   " + m.toString());

        }
     //   Observable ob = Observable.from(masterList).map(new Func1<Master, Object>() {
//            @Override
//            public Object call(Master master) {
//                Log.d("lihui", "master---   " + master.toString());
//
//                return master.getPetList();
//            }
//        });
        Observable ob = Observable.from(masterList).flatMap(new Func1<Master, Observable<Pet>>() {
            @Override
            public Observable<Pet> call(Master master) {
                Log.d("lihui", "master---   " + master.toString());

                return Observable.from(master.getPetList());
            }
        });

        Action1 action1 = new Action1<Pet>() {
            @Override
            public void call(Pet pet) {
                Log.d("lihui", "p---   " + pet.toString());
//                List<Pet> petlist = (List<Pet>) o;
//                for (Pet p : petlist) {
//                    Log.d("lihui", "p---   " + p.toString());
//                }
            }
        };

        Rx.click(bt, new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                ob.subscribe(action1);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null)
            unbinder.unbind();
    }

    class Master {
        public Master(String name) {
            this.mastername = name;
        }

        @Override
        public String toString() {
            return "Master{" +
                    "mastername='" + mastername + '\'' +
                    ", petList=" + petList +
                    '}';
        }

        public String getMastername() {
            return mastername;
        }

        public void setMastername(String mastername) {
            this.mastername = mastername;
        }

        String mastername;

        public List<Pet> getPetList() {
            return petList;
        }


        List<Pet> petList = new ArrayList<>();

    }

    class Pet {
        public String getPetname() {
            return petname;
        }

        public void setPetname(String petname) {
            this.petname = petname;
        }

        public Pet(String petname) {
            this.petname = petname;
        }

        @Override
        public String toString() {
            return "Pet{" +
                    "petname='" + petname + '\'' +
                    '}';
        }

        String petname;
    }
}


列印:

1、
Observable.from(masterList).flatMap(new Func1<Master, Observable<Pet>>()

對應----
 new Action1<Pet>() {
            @Override
            public void call(Pet pet) {
}

1 Master call --- 3次Action1 call
列印:
08-04 18:29:19.036 14475-14475/com.lcwr.android D/lihui: master---   Master{mastername='master0', petList=[Pet{petname='master0-pet0'}, Pet{petname='master0-pet1'}, Pet{petname='master0-pet2'}]}
08-04 18:29:19.040 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master0-pet0'}
08-04 18:29:19.040 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master0-pet1'}
08-04 18:29:19.040 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master0-pet2'}
08-04 18:29:19.040 14475-14475/com.lcwr.android D/lihui: 

master---   Master{mastername='master1', petList=[Pet{petname='master1-pet0'}, Pet{petname='master1-pet1'}, Pet{petname='master1-pet2'}]}
08-04 18:29:19.041 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master1-pet0'}
08-04 18:29:19.041 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master1-pet1'}
08-04 18:29:19.041 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master1-pet2'}
08-04 18:29:19.041 14475-14475/com.lcwr.android D/lihui: 

master---   Master{mastername='master2', petList=[Pet{petname='master2-pet0'}, Pet{petname='master2-pet1'}, Pet{petname='master2-pet2'}]}
08-04 18:29:19.041 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master2-pet0'}
08-04 18:29:19.041 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master2-pet1'}
08-04 18:29:19.042 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master2-pet2'}
08-04 18:29:19.042 14475-14475/com.lcwr.android D/lihui: 

master---   Master{mastername='master3', petList=[Pet{petname='master3-pet0'}, Pet{petname='master3-pet1'}, Pet{petname='master3-pet2'}]}
08-04 18:29:19.042 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master3-pet0'}
08-04 18:29:19.042 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master3-pet1'}
08-04 18:29:19.042 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master3-pet2'}
08-04 18:29:19.043 14475-14475/com.lcwr.android D/lihui: 

master---   Master{mastername='master4', petList=[Pet{petname='master4-pet0'}, Pet{petname='master4-pet1'}, Pet{petname='master4-pet2'}]}
08-04 18:29:19.043 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master4-pet0'}
08-04 18:29:19.043 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master4-pet1'}
08-04 18:29:19.043 14475-14475/com.lcwr.android D/lihui: p---   Pet{petname='master4-pet2'}

錯誤

        Observable ob = Observable.from(masterList).map(new Func1<Master, Observable<Pet>>() {}

new Action1<Pet>() {
            @Override
            public void call(Pet pet) {}}

                                                                   Caused by: java.lang.ClassCastException: rx.Observable cannot be cast to com.lcwr.android.modules.home.activity.Testactivity$Pet

修改map

        Observable ob = Observable.from(masterList).map(new Func1<Master, List<Pet>>() {}

----

new Action1<List<Pet>>() {
            @Override
            public void call(List<Pet> petList) {
                Log.d("lihui", "petList---   " + petList.toString());}}


列印: 1-1
map 1 ---   Action1 1
08-04 18:34:29.230 21992-21992/com.lcwr.android D/lihui: master---   Master{mastername='master0', petList=[Pet{petname='master0-pet0'}, Pet{petname='master0-pet1'}, Pet{petname='master0-pet2'}]}
08-04 18:34:29.230 21992-21992/com.lcwr.android D/lihui: petList---   [Pet{petname='master0-pet0'}, Pet{petname='master0-pet1'}, Pet{petname='master0-pet2'}]
08-04 18:34:29.230 21992-21992/com.lcwr.android D/lihui: master---   Master{mastername='master1', petList=[Pet{petname='master1-pet0'}, Pet{petname='master1-pet1'}, Pet{petname='master1-pet2'}]}
08-04 18:34:29.230 21992-21992/com.lcwr.android D/lihui: petList---   [Pet{petname='master1-pet0'}, Pet{petname='master1-pet1'}, Pet{petname='master1-pet2'}]
08-04 18:34:29.231 21992-21992/com.lcwr.android D/lihui: master---   Master{mastername='master2', petList=[Pet{petname='master2-pet0'}, Pet{petname='master2-pet1'}, Pet{petname='master2-pet2'}]}
08-04 18:34:29.231 21992-21992/com.lcwr.android D/lihui: petList---   [Pet{petname='master2-pet0'}, Pet{petname='master2-pet1'}, Pet{petname='master2-pet2'}]
08-04 18:34:29.231 21992-21992/com.lcwr.android D/lihui: master---   Master{mastername='master3', petList=[Pet{petname='master3-pet0'}, Pet{petname='master3-pet1'}, Pet{petname='master3-pet2'}]}
08-04 18:34:29.231 21992-21992/com.lcwr.android D/lihui: petList---   [Pet{petname='master3-pet0'}, Pet{petname='master3-pet1'}, Pet{petname='master3-pet2'}]
08-04 18:34:29.231 21992-21992/com.lcwr.android D/lihui: master---   Master{mastername='master4', petList=[Pet{petname='master4-pet0'}, Pet{petname='master4-pet1'}, Pet{petname='master4-pet2'}]}
08-04 18:34:29.231 21992-21992/com.lcwr.android D/lihui: petList---   [Pet{petname='master4-pet0'}, Pet{petname='master4-pet1'}, Pet{petname='master4-pet2'}]

同時看出

from 產生事件是 一次 傳送一個事件!!!

flatmap 相當於迴圈2次,開始一次迴圈,內部再迴圈一次!!!