3

我刚开始使用分页库,代码工作正常,但我不知道如何处理进度条和 Toast 错误消息等错误,所以我的代码是 repo

public class SearchRepository extends PageKeyedDataSource<Integer, Doc> {
public static final int PAGE_SIZE = 5;
private static final int FIRST_PAGE = 1;
private static final String token = "1234";
private static final String TAG = "www";

public SearchRepository() {
}

@Override
public void loadInitial(@NonNull final LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadInitial: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, FIRST_PAGE, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {

                        callback.onResult(response.body().getResult().getDocs(), null, FIRST_PAGE + 1);
                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {

                }
            });
}

@Override
public void loadBefore(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadBefore: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {
                        Integer key = (params.key > 1) ? params.key - 1 : null;
                        callback.onResult(response.body().getResult().getDocs(), key);

                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {
                }
            });
}

@Override
public void loadAfter(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
    Log.e(TAG, "loadAfter: ");
    APIService apiService = RetroClass2.getAPIService();
    apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
            .enqueue(new Callback<Products>() {
                @Override
                public void onResponse(Call<Products> call, Response<Products> response) {
                    if (response.body() != null) {

                        Integer key = response.body().getResult().getDocs().size() != 0 ? params.key + 1 : null;
                        callback.onResult(response.body().getResult().getDocs(), key);
                    }
                }

                @Override
                public void onFailure(Call<Products> call, Throwable t) {
                }
            });
}

这是我的工厂保存数据

public class SearchRepoFactory extends DataSource.Factory {
private MutableLiveData<PageKeyedDataSource<Integer, Doc>> listMutableLiveData = new MutableLiveData<>();


@Override
public DataSource create() {
    SearchRepository searchRepository = new SearchRepository();
    listMutableLiveData.postValue(searchRepository);
    return searchRepository;
}

public MutableLiveData<PageKeyedDataSource<Integer, Doc>> getListMutableLiveData() {
    return listMutableLiveData;
   }
}

这是我的视图模型

public class SearchViewModel extends ViewModel {
public LiveData<PagedList<Doc>> listLiveData;
private LiveData<PageKeyedDataSource<Integer, Doc>> liveData;
private static final String TAG = "yyy";

public SearchViewModel() {

    SearchRepoFactory factory = new SearchRepoFactory();
    liveData = factory.getListMutableLiveData();

    PagedList.Config config = (new PagedList.Config.Builder()).setEnablePlaceholders(false).setPageSize(SearchRepository.PAGE_SIZE).build();

    listLiveData = (new LivePagedListBuilder(factory, config)).build();
    }
}

我的适配器工作正常,所以我要跳过适配器

和这个活动

  recyclerView = findViewById(R.id.rv1);
    progressBar = findViewById(R.id.probar);
    final SearchAdapter adapter = new SearchAdapter(this);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    SearchViewModel searchViewModel = new SearchViewModel(this);

    searchViewModel.listLiveData.observe(this, new Observer<PagedList<Doc>>() {
        @Override
        public void onChanged(@Nullable PagedList<Doc> docs) {
            adapter.submitList(docs);
        }
    });
    recyclerView.setAdapter(adapter);

我要再说一遍,代码在没有错误的情况下工作正常,但我想处理无连接 Toast 和隐藏进度条等错误,而且我不希望用户公开静态,我在 SearchRepository 中获得服务器响应以及到 UI 的很长的路要走,我知道我的班级命名并不完美:),有什么想法吗?

4

0 回答 0