RxScala / Java - 为什么我的progressBar没有出现?

时间:2018-03-26 12:59:25

标签: android rx-java observable rx-scala

我有一个简单的应用程序SearchView,用户可以查询,并根据状态我显示/隐藏一些视图窗口小部件。我无法找到ProgressBar永远不可见的原因。有人可以说加载指示器为什么永远不会出现?

以下是代码:

trait MainView {

  def searchIntent(): Observable[String]

  def render(viewState: PartialMainViewState)
}

主讲人:

class MainPresenter(...) {

  def bind(mainView: MainView) = {
    val searchQueryObservable = mainView.searchIntent()
      .flatMap(s => mainInteractor.search(s))

    searchQueryObservable.observeOn(AndroidSchedulers.mainThread()).subscribe { state => mainView.render(state) }
  }
}

具有实际渲染的活动:

class SimpleActivity extends BaseSearchActivity with MainView {

  override def searchIntent(): Observable[String] = {
    Observable.defer {
      RxSearchView.queryTextChanges(vh.searchView)
        .filter((ev: CharSequence) => ev.length >= 2 || ev.length == 0)
        .debounce(500.milliseconds)
        .map[String]((ev: CharSequence) => ev.toString())
    }
  }

  override def render(viewState: PartialMainViewState): Unit =
    viewState match {
     case SearchNotStartedYet => renderSearchNotStarted()
     case Loading => renderLoading()
     case EmptyResult(_) => renderEmptyResult()
     case SearchResult(_, res) => renderResult(res)
     ...
    }

  def renderSearchNotStarted(): Unit = {
    vh.list.setVisibility(View.GONE)
    vh.progressBar.setVisibility(View.GONE)
    vh.emptyView.setVisibility(View.GONE)
  }

  def renderLoading(): Unit = {
    vh.list.setVisibility(View.GONE)
    vh.progressBar.setVisibility(View.VISIBLE)
    vh.emptyView.setVisibility(View.GONE)
  }

  def renderEmptyResult(): Unit = {
    vh.list.setVisibility(View.GONE)
    vh.progressBar.setVisibility(View.GONE)
    vh.emptyView.setVisibility(View.VISIBLE)
  }

  def renderResult(result: Array[String]): Unit = {
    vh.list.setVisibility(View.VISIBLE)
    vh.progressBar.setVisibility(View.GONE)
    vh.emptyView.setVisibility(View.GONE)
    //setting the adapter etc
  }

}

我在这里模拟网络请求:

class SimpleInteractor(...) {
  def search(query: String): Observable[PartialMainViewState] = {
    Thread.sleep(1500) // to simulate "Network latency"

    if(query.isEmpty) Observable.just(SearchNotStartedYet)

    else Observable.just(persons.filter(_.toLowerCase().contains(query.toLowerCase())))
      .map(array => {
        if(array.isEmpty) EmptyResult(query) else SearchResult(query, array)
      }).+:(Loading).onErrorReturn(error => ErrorState(query, error))
  }
}

1 个答案:

答案 0 :(得分:1)

在演示者中,尝试通过添加searchQueryObservable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { state => mainView.render(state) } 运算符

订阅不同的主题
can't load package: package fantasy_stats: found packages jsontest (jsontest.go) and main (main.go) in /home/scott/programming/golang/src/fantasy_stats