Android分页库,Endless RecyclerView,改造

问题描述

我有一个EditText和一个RecyclerView。我想观察EditText并将数据从TMDB API获取到无尽的RecyclerView。我可以使用Retrofit和Paging库将数据从API获取到无尽的Recyclerview。但是我只能从viewModel向API发送查询,我想观察EditText并将查询发送给API。我该如何实现? ViewModel:

class AddViewModel(private val movieRepository : MoviePagedListRepository) : ViewModel() {
    val isError = MutableLiveData<Boolean>()
    val isLoading = MutableLiveData<Boolean>()
    private val compositeDisposable = CompositeDisposable()
    val movieSeriesETContent = MutableLiveData<String>()
    
    val  moviePagedList : LiveData<PagedList<Movie>> by lazy {
        movieRepository.fetchLiveMoviePagedList("break",compositeDisposable)
    }

    override fun onCleared() {
        super.onCleared()
        compositeDisposable.dispose()
    }
}

片段:

vm.movieSeriesETContent.observe(viewLifecycleOwner,Observer {
            Log.d("Murad",it)
        })

vm.moviePagedList.observe(viewLifecycleOwner,Observer {
            adapter.submitList(it)
        })

您可以在ViewModel类中看到请求“ break”,但是当我观察EditText放置Log.d(“ Murad”,it)的位置时,我想发送查询。

解决方法

您应将EditText替换为SearchView并应用OnQueryTextListener。这样就不需要观察者更改文本了。

searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            //you can show a progress bar here as well
            progressBar.setIndeterminate(true);

            // here you can call your ViewModel's searchQueryMethod()
          
            return true;
        }
});

在您的观察者中,您仅需要适配器上的更改数据集。我确实也使用TMDB API创建了app,这也是我获取电影列表的方法。

快乐编码!

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...