有一个类别列表(A,B,C),每个列表都有一个子类别列表(A1,A2),(B1,B2),(C1,C2),每个子类别都有一个项目列表下载(item_a11,item_a12),(item_a21,item_a22),(item_b11,item_b12)等。因此,我需要按以下顺序逐个加载项目:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@null"
android:gravity="center"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="3dp"/>
是否可以使用RxJava实现?如果是这样,我会非常感谢任何建议!
答案 0 :(得分:0)
你可以做这样的事情
1)make method that returns List of A(getListOfA).
2)now getListofA.subscribe().
3)now on onNext() call getListOfA1() that return single value using fromIterable()(i.e. return single item from A1.
4)now on getListofA1().subscribe()'s onNext you can do what you want.
答案 1 :(得分:0)
假设您的类很相似,您可以尝试这个解决方案。它是逐个下载的项目,如果没有空格,则会抛出异常,因此不会再进行下载尝试。
public interface Model {
Single<String> download(String item);
Single<List<Category>> categories();
Single<Boolean> availableSpace();
}
public class Category {
public List<Subcategory> subcategories;
public List<Subcategory> getSubcategories() {
return subcategories;
}
}
public class Subcategory {
public List<String> items;
public List<String> getItems() {
return items;
}
}
private Model model;
public void downloadAll() {
model.categories()
.flatMapObservable(Observable::fromIterable)
.map(Category::getSubcategories)
.flatMap(Observable::fromIterable)
.map(Subcategory::getItems)
.flatMap(Observable::fromIterable)
.flatMapSingle(item -> model.availableSpace()
.flatMap(available -> {
if (available) {
return model.download(item);
} else {
return Single.error(new IllegalStateException("not enough space"));
}
}))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(item -> {}, throwable -> {});
}