分页体系结构组件的初始占位符

ofa*_*vai 5 android android-recyclerview android-architecture-components

我在项目中使用Paging Architecture Components来从网络中加载列表(尚未使用数据库)。我DataSource是一个PositionalDataSource子类,我PagedList.Config看起来像这样:

PagedList.Config config = new PagedList.Config.Builder()
        .setPageSize(10)
        .setInitialLoadSizeHint(20)
        .setPrefetchDistance(20)
        .setEnablePlaceholders(true)
        .build();
Run Code Online (Sandbox Code Playgroud)

启用了占位符,并且我在PagedListAdapter子类中处理了null ViewHolders以不同方式显示占位符项。当在列表末尾加载其他项时,占位符可以正常工作,但是我还想在加载前几项之前显示一个占位符项(例如常规加载指示器)。

分页库有办法做到这一点吗?我尝试LoadInitialCallback.onResult()1代替的位置进行调用0,但这只会在第一个实际项目之前创建一个占位符项目,并且该占位符项目永远不会消失。

Dan*_*ico 0

有一种方法可以传递数据集大小,我认为这就是您所缺少的。

public abstract static class LoadInitialCallback<Key, Value> {
        /**
         * Called to pass initial load state from a DataSource.
         * <p>
         * Call this method from your DataSource's {@code loadInitial} function to return data,
         * and inform how many placeholders should be shown before and after. If counting is cheap
         * to compute (for example, if a network load returns the information regardless), it's
         * recommended to pass data back through this method.
         * <p>
         * It is always valid to pass a different amount of data than what is requested. Pass an
         * empty list if there is no more data to load.
         *
         * @param data List of items loaded from the DataSource. If this is empty, the DataSource
         *             is treated as empty, and no further loads will occur.
         * @param position Position of the item at the front of the list. If there are {@code N}
         *                 items before the items in data that can be loaded from this DataSource,
         *                 pass {@code N}.
         * @param totalCount Total number of items that may be returned from this DataSource.
         *                   Includes the number in the initial {@code data} parameter
         *                   as well as any items that can be loaded in front or behind of
         *                   {@code data}.
         */
        public abstract void onResult(@NonNull List<Value> data, int position, int totalCount,
                @Nullable Key previousPageKey, @Nullable Key nextPageKey);
Run Code Online (Sandbox Code Playgroud)

我尝试过,但也看不到预览。

检查http://youtube.com/watch?v=BE5bsyGGLf4

希望这可以帮助