猿问

loadInitial 方法未在 PositionalDataSource<Item>

我正在从Java中的分页库实现PositionalDataSource,并遇到一个问题,即LocationalDataSource的子类的构造函数被调用,但在此之后,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));

}

这是我的页面列表配置


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

    }

}


慕神8447489
浏览 366回答 2
2回答

函数式编程

负载大小和偏移量通过设置,因此您无需自己计算负载范围。PagedList.Config更改函数loadInitial@Overridepublic void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {&nbsp; &nbsp; Log.d("PaginationDataSource", "loadInitial");&nbsp; &nbsp; if (callLogs!=null && !callLogs.isEmpty())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; callback.onResult(loadRangeInternal(0, params.requestedLoadSize), 0);&nbsp; &nbsp; }}编辑:请同时尝试此配置&nbsp;PagedList.Config config =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new PagedList.Config.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setPageSize(50)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setEnablePlaceholders(false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setPrefetchDistance(25)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();编辑2:尝试将扩展从 更改为 和 更改为DataSource.FactoryDataSource.Factory<Integer, ModelClass>PositionalDataSourcePositionalDataSource<ModelClass>

叮当猫咪

经过太多的挣扎,我能够解决我的问题。问题出在我的getPagedCallLog方法上。我写道:public synchronized LiveData<PagedList<CallTable>> getPagedCallLogs() {if (pagedCallLogs!=null && pagedCallLogs.getValue()!=null){&nbsp; &nbsp; Log.d("PagingGetData", "Done");&nbsp; &nbsp; return pagedCallLogs;}else{&nbsp; &nbsp; Log.d("PagingGetData", "Null");&nbsp; &nbsp; return null;}}我正在学习Google I / O '18,他说loadInitial是由pageList调用的,然后我意识到在我的情况下它没有被调用。在删除pagedCallLogs.getValue()!=null后,它工作正常,这是我的愚蠢错误。现在它看起来像这样:public synchronized LiveData<PagedList<CallTable>> getPagedCallLogs() {if (pagedCallLogs!=null){&nbsp; &nbsp; Log.d("PagingGetData", "Done");&nbsp; &nbsp; return pagedCallLogs;}else{&nbsp; &nbsp; Log.d("PagingGetData", "Null");&nbsp; &nbsp; return null;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答