我实际上正在使用Triposo的API来显示国家/地区列表。 https://www.triposo.com/api/
但是,在回复中,没有数字ID,我在增加页面大小时遇到了困难。我应该对这些回应做些什么来克服?例如,Github API完全适用于Paging Library,因为它将itemId作为数字返回。
响应示例
{
"results": [
{
"name": "Benin",
"country_id": "Benin",
"snippet": "A safe and relatively easy country for travellers to visit; birthplace of the Voodoo religion and former home of the Kingdom of Dahomey.",
"parent_id": null,
"score": 3.98127216481287,
"id": "Benin"
}
],
"estimated_total": 30845,
"more": true
}
ItemKeyedDataSource.java
public class ItemKeyedCountryDataSource extends ItemKeyedDataSource<Integer, CountryResult> {
private EndpointHelper endpointHelper;
ItemKeyedCountryDataSource() {
endpointHelper = EndpointHelper.getInstance();
}
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<CountryResult> callback) {
final List<CountryResult> countryResultList = new ArrayList<>();
endpointHelper.getCountryList(0)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retry(1)
.onErrorResumeNext(new Func1<Throwable, Observable<? extends CountryWrapper>>() {
@Override
public Observable<? extends CountryWrapper> call(Throwable throwable) {
return Observable.error(throwable);
}
})
.subscribe(new Subscriber<CountryWrapper>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(CountryWrapper countryWrapper) {
if (countryWrapper.getResults().size() > 0) {
countryResultList.addAll(countryWrapper.getResults());
callback.onResult(countryResultList);
}
}
});
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull final LoadCallback<CountryResult> callback) {
final List<CountryResult> countryResultList = new ArrayList<>();
endpointHelper.getCountryList(params.requestedLoadSize)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retry(1)
.onErrorResumeNext(new Func1<Throwable, Observable<? extends CountryWrapper>>() {
@Override
public Observable<? extends CountryWrapper> call(Throwable throwable) {
return Observable.error(throwable);
}
})
.subscribe(new Subscriber<CountryWrapper>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(CountryWrapper countryWrapper) {
if (countryWrapper.getResults().size() > 0) {
countryResultList.addAll(countryWrapper.getResults());
callback.onResult(countryResultList);
}
}
});
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<CountryResult> callback) {
//Do nothing
}
@NonNull
@Override
public Integer getKey(@NonNull CountryResult item) {
return ?; // what do I return here?
}
}
ViewModel.java
public class SearchableActivityViewModel extends ViewModel {
private LiveData<PagedList<CountryResult>> countryResult;
SearchableActivityViewModel() {
Executor executor = Executors.newFixedThreadPool(5);
CountryResultDataSourceFactory countryResultDataSourceFactory = new CountryResultDataSourceFactory();
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(20) //first load
.setPageSize(21)
.build();
//noinspection unchecked
countryResult = new LivePagedListBuilder<String, CountryResult>(countryResultDataSourceFactory, config)
.setFetchExecutor(executor)
.build();
}
public LiveData<PagedList<CountryResult>> getCountryResult(){
return countryResult;
}
@Override
protected void onCleared() {
super.onCleared();
}
}
提前致谢。希望我很清楚。
最佳,