加载数据时分页库网络错误

时间:2018-09-11 14:12:34

标签: android android-paging

我正在使用分页库,而我在LiveData PagedList中苦苦挣扎。 我的分页列表可以正常工作,但是有一种情况是不行的。当列表中已显示数据时,我有RecyclerView + SwipeRefreshLayout,并且我禁用了网络,我尝试滑动刷新以删除列表中的所有数据。我该如何避免呢?

public class InterestsViewModel extends ViewModel {

private LiveData<PagedList<InterestItem>> mListInterests;
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();

private static final int PAGE_SIZE = 15;

private GroupsInterestsDataSourceFactory mGroupsInterestsDataSourceFactory;

public InterestsViewModel() {
    Executor executor = Executors.newFixedThreadPool(5);

    mGroupsInterestsDataSourceFactory = new GroupsInterestsDataSourceFactory(mCompositeDisposable);
    PagedList.Config config = new PagedList.Config.Builder()
            .setPageSize(PAGE_SIZE)
            .setInitialLoadSizeHint(PAGE_SIZE)
            .setEnablePlaceholders(true)
            .build();

    mListInterests = new LivePagedListBuilder<>(mGroupsInterestsDataSourceFactory,config)
            .setFetchExecutor(executor)
            .build();
}

public void retry() {
    mGroupsInterestsDataSourceFactory.getGroupsInterestsDataSource().getValue().retry();
}

public void refresh() {
    mGroupsInterestsDataSourceFactory.getGroupsInterestsDataSource().getValue().invalidate();
}

public LiveData<NetworkState> getNetworkState() {
    return Transformations.switchMap(mGroupsInterestsDataSourceFactory.getGroupsInterestsDataSource(), new Function<GroupsInterestsDataSource, LiveData<NetworkState>>() {
        @Override
        public LiveData<NetworkState> apply(GroupsInterestsDataSource input) {
            return input.getNetworkState();
        }
    });
}

public LiveData<NetworkState> getRefreshState() {
    return Transformations.switchMap(mGroupsInterestsDataSourceFactory.getGroupsInterestsDataSource(), new Function<GroupsInterestsDataSource, LiveData<NetworkState>>() {
        @Override
        public LiveData<NetworkState> apply(GroupsInterestsDataSource input) {
            return input.getInitialLoad();
        }
    });
}

public LiveData<PagedList<InterestItem>> getListInterests() {
    return mListInterests;
}

@Override
protected void onCleared() {
    super.onCleared();
    mCompositeDisposable.dispose();
}
}

从片段开始:

private void initAdapter() {
    mAdapter = new InterestsAdapter(this);
    mListInterest.setAdapter(mAdapter);

    mInterestsViewModel.getListInterests().observe(this, new Observer<PagedList<InterestItem>>() {
        @Override
        public void onChanged(@Nullable PagedList<InterestItem> interestItems) {
            mAdapter.submitList(interestItems);
        }
    });
    mInterestsViewModel.getNetworkState().observe(this, new Observer<NetworkState>() {
        @Override
        public void onChanged(@Nullable NetworkState networkState) {
            mAdapter.setNetworkState(networkState);
        }
    });
    mInterestsViewModel.getRefreshState().observe(this, new Observer<NetworkState>() {
        @Override
        public void onChanged(@Nullable NetworkState networkState) {
            if (networkState != null) {
                if (mAdapter.getCurrentList() != null) {
                    if (networkState.getStatus()
                            == NetworkState.LOADED.getStatus() || networkState.getStatus()
                            == Status.FAILED)
                        mIsRefreshing = false;

                    if (mAdapter.getCurrentList().size() > 0) {
                        mSwipeRLInterests.setRefreshing(networkState.getStatus()
                                == NetworkState.LOADING.getStatus());
                    }
                }
                setInitialLoadingState(networkState);
            }
        }
    });
}
@Override
public void retry() {
    mInterestsViewModel.retry();
}

private void initSwipeToRefresh() {
    mSwipeRLInterests.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mIsRefreshing = true;
            mInterestsViewModel.refresh();
        }
    });
    mSwipeRLInterests.setColorSchemeColors(getResources().getColor(R.color.colorPrimaryDark)
            , getResources().getColor(R.color.colorPrimary)
            , getResources().getColor(R.color.colorPrimaryDark));

}

0 个答案:

没有答案