1

我是 Android Paging 的新手(不要拍我),我正在尝试使用 Database 和 Network 来实现 Paging,比如android sample

我在片段中观察我的 ViewModel 的 getPagedList() ,然后我将列表提交给我的适配器,如下所示:

public class MyFragment extends Fragment{
...

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        refreshLayout.setOnRefreshListener(this::requestRefresh);

        myViewModel.getPagedList().observe(this, myList -> {
            myAdapter.submitList(myList);
        });

    }

    private void requestRefresh(){
        if (myAdapter != null && myAdapter.getCurrentList() != null) {
            myAdapter.getList().getDataSource().invalidate();
        }

        myViewModel.requestRefresh(this);
    }

}

这是我的 ViewModel 以及我如何创建 PagedList:

public class MyViewModel extends ViewModel {
...

    @Inject
    public MyViewModel(MyRepository repository) {
        this.repository = repository;
        initPagedList();

    }

    private void initPagedList() {
        DataSource.Factory<Integer, MyModel> factory = repository.getCurrentList();
        PagingBoundaryCallback boundaryCallback = new PagingBoundaryCallback();

        PagedList.Config config = (new PagedList.Config.Builder())
                .setPageSize(10)
                .setInitialLoadSizeHint(20)
                .build();

        mPagedList = new LivePagedListBuilder<>(factory, config)
                .setBoundaryCallback(boundaryCallback)
                .build();
    }



    public LiveData<PagedList<MyModel>> getPagedList() {
        return mPagedList;
    }

    public void requestRefresh() {
        initPagedList();
    }

}


这是我的 BoundaryCallback:

public class PagingBoundaryCallback extends PagedList.BoundaryCallback<MyModel> {
    private MyRepository repository;
    private boolean isLoading = false;
    private boolean allItemsAreLoaded = false;
    private int currentPage = 1;

    public PagingBoundaryCallback() {
        repository = MyApplication.getInstance().getAppComponent().MyRepository();

    }

    @Override
    public void onZeroItemsLoaded() {
        allItemsAreLoaded = false;
        currentPage = 1;
        requestFetch(1);

    }

    @Override
    public void onItemAtEndLoaded(@NonNull MyModel itemAtEnd) {
        if (!isLoading && !allItemsAreLoaded){
            requestFetch(currentPage);
        }

    }

    private void requestFetch(int page) {
        isLoading = true;
        repository.requestFetch(page, new FinishCallback() {
            @Override
            public void onFinish(List<MyModel> myList) {
                currentPage++;
                isLoading = false;

                if (myList.size() == 0)
                    allItemsAreLoaded = true;
            }

            @Override
            public void onError(String message) {
                isLoading = false;
            }
        });
    }
}

附加信息:

  1. repository.requestFetch() 从 Web 服务请求某个页面,并且响应将插入到数据库(Room)中。如果响应是针对第一页的,则在插入新数据之前将删除所有旧数据。

  2. repository.getCurrentList() 返回由数据库 dao 创建的 DataSource.Factory

问题是,当我刷新列表时,BoundaryCallback 中的变量没有更新,这意味着“currentPage”继续增加,并且它没有重置为 1(显然,每次调用 initPagedList 都会创建一个新的 BoundaryCallback)。无论如何,我不知道我在这里缺少什么,任何帮助将不胜感激。

4

0 回答 0