-1 search android android-jetpack android-paging
我正在使用 Android 分页库,如下所述:https : //developer.android.com/topic/libraries/architecture/paging.html
但我也有一个 EditText 用于按名称搜索国家/地区。
如何过滤 Paging 库中的结果以仅显示匹配的国家/地区?
public final LiveData> countriesPagedList;
public AllCountriesViewModel(@NonNull Application application) {
super(application);
appRepository = new AppRepository(application);
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(true)
.setPageSize(30)
.setInitialLoadSizeHint(10)
.setPrefetchDistance(50)
.build();
countriesPagedList = new LivePagedListBuilder(appRepository.getAllCountries(),config).build();
}
Run Code Online (Sandbox Code Playgroud)
假设你对分页库、房间和接收有合理的了解,那么需要做几件事:
private final LiveData<PagedList<YourEntity>> yourEntities;private final MutableLiveData<String> filterText = new MutableLiveData<>();以下是将它们绑定在一起的方法:
this.yourEntities = Transformations.switchMap(
filterText, (Function<String, LiveData<PagedList<YourEntity>>>) input -> {
if (StringUtils.isBlank(input)) {
return new LivePagedListBuilder(
yourEntitiesManagementDao.queryYourEntities(), PAGE_SIZE).build();
} else {
return new LivePagedListBuilder(
yourEntitiesManagementDao.queryYourEntitiesFiltered("%" + input + "%"), PAGE_SIZE).build();
}
}
);
Run Code Online (Sandbox Code Playgroud)this.filterText.setValue("");public void setFilter(String filter) {
filterText.setValue(filter);
}基本上,通过上述设置,您只需要setFilter使用 EditText 中的文本。
| 归档时间: |
|
| 查看次数: |
3130 次 |
| 最近记录: |