欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

项目rxjava+rxandroid学习

程序员文章站 2022-06-07 12:58:51
...

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次,开始一次循环,内部再循环一次!!!

相关标签: map flatmap