gpt4 book ai didi

android - RxScala/Java - 为什么我的 progressBar 没有显示?

转载 作者:行者123 更新时间:2023-11-30 00:03:19 24 4
gpt4 key购买 nike

我有一个带有 SearchView 的简单应用程序,用户可以查询该应用程序并根据状态显示/隐藏一些 View 小部件。我无法找出为什么 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) }
}
}

具有实际呈现的 Activity:

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))
}
}

最佳答案

在您的演示者中,尝试通过添加 subscribeOn 运算符在不同的线程上订阅:

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

关于android - RxScala/Java - 为什么我的 progressBar 没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49492087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com