我正在用Java的Paging库实现PositionalDataSource,并遇到一个问题,即调用了PositionalDataSource的子类的构造函数,但没有调用loadInitial方法。
public HistoryPositionalDataSource(List<CallTable> callLogs)
{
this.callLogs = callLogs;
Log.d("PaginationDataSource", "Constructor");
}
@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
Log.d("PaginationDataSource", "loadInitial");
if (callLogs!=null && !callLogs.isEmpty())
{
int totalCount = computeCount();
int position = computeInitialLoadPosition(params, totalCount);
int loadSize = computeInitialLoadSize(params, position, totalCount);
callback.onResult(loadRangeInternal(position, loadSize), position, totalCount);
}
}
@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback callback) {
callback.onResult(loadRangeInternal(params.startPosition, params.loadSize));
}
这是我的PageListConfig
private void init() {
pagedListConfig = (new PagedList.Config.Builder()).setEnablePlaceholders(true)
.setInitialLoadSizeHint(Integer.MAX_VALUE).setPageSize(Integer.MAX_VALUE).build();
Executor executor = Executors.newFixedThreadPool(3);
List<CallTable> listLogs = getCallLogs(context);
historyDataSourceFactory = new HistoryDataSourceFactory(listLogs);
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(historyDataSourceFactory, pagedListConfig);
pagedCallLogs = livePagedListBuilder
.setFetchExecutor(executor)
.build();
}
工厂类:
public class HistoryDataSourceFactory extends DataSource.Factory {
private static final String TAG = HistoryDataSourceFactory.class.getSimpleName();
private HistoryPositionalDataSource historyPositionalDataSource;
public HistoryDataSourceFactory(List<CallTable> callLogs)
{
if (callLogs!=null && !callLogs.isEmpty())
{
Log.d("PaginationFactory", "NotNullLogs");
historyPositionalDataSource = new HistoryPositionalDataSource(callLogs);
}
}
@Override
public DataSource create() {
return historyPositionalDataSource;
}
}
我的getPagedCallLogs方法:
public synchronized LiveData<PagedList<CallTable>> getPagedCallLogs() {
if (pagedCallLogs!=null && pagedCallLogs.getValue()!=null)
{
Log.d("PagingGetData", "Done");
return pagedCallLogs;
}
else
{
Log.d("PagingGetData", "Null");
return null;
}
}
日志图像如下。
答案 0 :(得分:2)
设置了负载大小和偏移
通过PagedList.Config
进行操作,因此您无需自己计算负载范围。
更改您的loadInitial
功能
@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
Log.d("PaginationDataSource", "loadInitial");
if (callLogs!=null && !callLogs.isEmpty())
{
callback.onResult(loadRangeInternal(0, params.requestedLoadSize), 0);
}
}
编辑: 也尝试此配置
PagedList.Config config =
new PagedList.Config.Builder()
.setPageSize(50)
.setEnablePlaceholders(false)
.setPrefetchDistance(25)
.build();
Edit2:
尝试将扩展范围从DataSource.Factory
更改为DataSource.Factory<Integer, ModelClass>
,将扩展范围从PositionalDataSource
更改为PositionalDataSource<ModelClass>
答案 1 :(得分:0)
经过太多的努力,我才能够解决我的问题。问题出在我的getPagedCallLog方法上。 我写道:
public synchronized LiveData<PagedList<CallTable>> getPagedCallLogs() {
if (pagedCallLogs!=null && pagedCallLogs.getValue()!=null)
{
Log.d("PagingGetData", "Done");
return pagedCallLogs;
}
else
{
Log.d("PagingGetData", "Null");
return null;
}
}
我正在使用Google I / O '18,他在其中说loadInitial由pageList调用,然后我意识到在我的情况下没有被调用。在删除pagedCallLogs.getValue()!= null之后,它工作正常,这是我的愚蠢错误。
现在看起来像这样:
public synchronized LiveData<PagedList<CallTable>> getPagedCallLogs() {
if (pagedCallLogs!=null)
{
Log.d("PagingGetData", "Done");
return pagedCallLogs;
}
else
{
Log.d("PagingGetData", "Null");
return null;
}
}