PositionalDataSource<Item> 中未调用 loadInitial 方法

loadInitial method not getting called in PositionalDataSource<Item>

我在 Java 中从 Paging Library 实现 PositionalDataSource 并遇到一个问题,即 PositionalDataSource 的子 class 的构造函数被调用,但之后 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();
}

工厂class:

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;
    }
}

日志图片如下。

加载大小和偏移量已设置 通过 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();

编辑2: 尝试将扩展名从 DataSource.Factory 更改为 DataSource.Factory<Integer, ModelClass>,将 PositionalDataSource 更改为 PositionalDataSource<ModelClass>

经过太多的挣扎,我终于能够解决我的问题了。问题出在我的 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;
}
}