Paging 3 简单使用

Paging 3 简单使用

需求: *常规列表,有刷新,加载(异常,底部)

依赖:

	//Paging
    def paging_version = "3.0.1"
    implementation "androidx.paging:paging-runtime:$paging_version"
    implementation "androidx.paging:paging-guava:$paging_version"
    testImplementation "androidx.paging:paging-common:$paging_version"

布局

 <com.scwang.smart.refresh.layout.SmartRefreshLayout>
 	<androidx.recyclerview.widget.RecyclerView/>
 </com.scwang.smart.refresh.layout.SmartRefreshLayout>

展示代码

		recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        _adapter = new InnerAdapter();
        //刷新UI
        _adapter.addLoadStateListener(new SmartLoadStateListener(_adapter, _binding.refresh));
        //加载更多UI
        ConcatAdapter adapter = _adapter.withLoadStateFooter(new LoadMoreStateAdapter(getLayoutInflater(), _adapter));
        _binding.recyclerView.setAdapter(adapter);

        _viewModel.getOrders().observe(getViewLifecycleOwner(), result -> {
            _adapter.submitData(getLifecycle(), result);
        });

SmartLoadStateListener:响应刷新UI

@Override
    public Unit invoke(CombinedLoadStates combinedLoadStates) {
        LoadState refresh = combinedLoadStates.getRefresh();
        if (refresh instanceof LoadState.Loading) {
            _layout.autoRefreshAnimationOnly();
        } else {
            _layout.finishRefresh();
        }
        return null;
    }

LoadMoreStateAdapter:让没有更多的状态可以展示

@Override
    public boolean displayLoadStateAsItem(@NonNull LoadState loadState) {
        return super.displayLoadStateAsItem(loadState)
                || (loadState instanceof LoadState.NotLoading && loadState.getEndOfPaginationReached());
    }

ViewModel:提供数据给View

public LiveData<PagingData<ExceptionOrderModel>> getOrders() {
        if (null == data) {
            Pager<Integer, _ExceptionOrder> pager = this.orderRepository.getExceptionOrders(30);
            CoroutineScope viewModelScope = ViewModelKt.getViewModelScope(this);
            LiveData<PagingData<_ExceptionOrder>> source = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), viewModelScope);
            data = Transformations.map(source, pagingData -> PagingDataTransforms.map(pagingData, AppAgent.getInstance().getWorkExecutor(), ExceptionOrderModel::new));
        }
        return data;
    }

PagingSource

 @Nullable
    @Override
    public Integer getRefreshKey(@NonNull PagingState<Integer, _ExceptionOrder> pagingState) {
        return null;
    }

    @NonNull
    @Override
    public ListenableFuture<LoadResult<Integer, _ExceptionOrder>> loadFuture(@NonNull LoadParams<Integer> loadParams) {
        int page = loadParams.getKey();
        int size = loadParams.getLoadSize();
        ListenableFuture<Result<List<_ExceptionOrder>>> task = Futures.submit(() -> dataSource.loadExceptionOrders(page, size), executor);
        return Futures.transform(task, input -> toLoadResult(input, page, size), executor);
    }
    
    private LoadResult<Integer, _ExceptionOrder> toLoadResult(Result<List<_ExceptionOrder>> result, int page, int size) {
        if (result instanceof Result.Success) {
            Result.Success<List<_ExceptionOrder>> success = (Result.Success<List<_ExceptionOrder>>) result;
            if (success.getData().size() == size) {
                return new LoadResult.Page<>(
                        success.getData()
                        , null
                        , page + 1
                );
            } else {
                return new LoadResult.Page<>(
                        success.getData()
                        , null
                        , null
                );
            }
        } else {
            Result.Fail<List<_ExceptionOrder>> fail = (Result.Fail<List<_ExceptionOrder>>) result;
            return new LoadResult.Error<>(new LoadErrorException(fail.getMsg()));
        }
    }

需要注意的一点,_adapter.withLoadStateFooter() 会返回一个Adapter,将这个Adapter绑定给recyclerView,才能展示Footer 或者 Headter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值