Paging Library without Room

前端 未结 2 1893
星月不相逢
星月不相逢 2021-02-20 03:11

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.

Here is

2条回答
  •  攒了一身酷
    2021-02-20 03:23

    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

    Also, turnoff placeholders:

    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 {
    
    @Override
    public int countItems() {
     return DataSource.COUNT_UNDEFINED;
    }
    
    @Override
    public List loadRange(int startPosition, int count) {
    
     Call call = NetworkModule.providesWebService().makeRequest();
     Response response = call.execute();
     return parseJson(response.body());
    }
    

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

提交回复
热议问题