没有房间的分页库 [英] Paging Library without Room

查看:134
本文介绍了没有房间的分页库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的分页库的所有示例均已包含在Room库中,并且Room为我们创建了一个数据源。在我自己的情况下,我需要创建自定义数据源。

All examples of the new paging library have been with Room library and Room creates a Data Source for us. In my own case, I need to create my custom data source.

这是我的视图模型类中的一种应返回实时数据的方法。我的livedata始终返回null。

Here is a method in my view model class that ought to return the live data. My livedata always returns null.

 LiveData<PagedList<ApiResult>> getData(){

    LivePagedListProvider<Integer,ApiResult> p = new LivePagedListProvider<Integer, ApiResult>() {
        @Override
        protected DataSource<Integer, ApiResult> createDataSource() {
            return new DataClass();
        }

    };

    listLiveData = p.create(0,new PagedList.Config.Builder()
            .setPageSize(5) //number of items loaded at once
            .setPrefetchDistance(0)// the distance to the end of already loaded list before new data is loaded
            .build());
    return listLiveData;
}

这是数据类

public class DataClass extends TiledDataSource<ApiResult> {

    private List<ApiResult> result = new ArrayList<>();

    @Override
    public int countItems() {
        return result.size();
    }

    @Override
    public List<ApiResult> loadRange(int startPosition, int count) {

        Call<String> call = NetworkModule.providesWebService().makeRequest();
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                Log.i(DataClass.this.getClass().getSimpleName() + " - onResponse", String.valueOf(response));
                result = parseJson(response.body());
            }

            @Override
            public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
                Log.i(DataClass.this.getClass().getSimpleName() + " - onFailure", t.getMessage());
            }

        });

        return result;
    }

}


推荐答案

我认为这可以帮助您:

1. countItems()应该返回DataSource.COUNT_UNDEFINED

2. loadRange(int startPosition,int count):您可以直接执行查询。

3.现在,您可以删除结果全局变量

I think this can help:
1. countItems() should return DataSource.COUNT_UNDEFINED
2. loadRange(int startPosition, int count): You can directly execute your query.
3. So now you can delete result global variable

此外,请关闭占位符:

listLiveData = p.create(0,new PagedList.Config.Builder()
        .setPageSize(5) //number of items loaded at once
        .setPrefetchDistance(10) //Must be >0 since placeholders are off
        .setEnablePlaceholders(false)
        .build());

以下是数据类的更新代码:

Here is the updated code for Data Class:

public class DataClass extends TiledDataSource<ApiResult> {

@Override
public int countItems() {
 return DataSource.COUNT_UNDEFINED;
}

@Override
public List<ApiResult> loadRange(int startPosition, int count) {

 Call<String> call = NetworkModule.providesWebService().makeRequest();
 Response<String> response = call.execute();
 return parseJson(response.body());
}

您可以在此处查看示例项目:
https://github.com/brainail/.samples/tree/master/ArchPagingLibraryWithNetwork

You can check an example project here: https://github.com/brainail/.samples/tree/master/ArchPagingLibraryWithNetwork

这篇关于没有房间的分页库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆