How to implement SwipeRefreshLayout with the new P

2020-02-09 07:36发布

问题:

I got an activity that shows a list of items to the user and it uses the Paging Library. My problem is that I can't reload the list when user swipes down the screen so that it fetches data from the server again.

Here is my DataSource Factory:

public class CouponListDataSourceFactory extends DataSource.Factory {
    private CouponListDataSource dataSource;

    public CouponListDataSourceFactory(CouponRepository repository, String token, String vendorId) {
        dataSource = new CouponListDataSource(repository, token, vendorId);
    }

    @Override
    public DataSource create() {
        return dataSource;
    }
}

And here's how I create the PagedList

PagedList.Config config = new PagedList.Config.Builder()
                .setInitialLoadSizeHint(15)
                .setPageSize(10)
                .build();
LiveData<PagedList<Coupon>> couponsLiveData = new LivePagedListBuilder<>(dataSourceFactory, config).build();

回答1:

After call mDataSource.invalidate() method, mDataSource will be invalidated and the new DataSource instance will be created via DataSource.Factory.create() method, so its important to provide new DataSource() instance every time inside DataSource.Factory.create() method, do not provide same DataSource instance every time.

mDataSource.invalidate() is not working, because after invalidation, CouponListDataSourceFactory provides the same, already invalidated DataSource instance.

After modification CouponListDataSourceFactory will be looked like in below smple, and call to mCouponListDataSourceFactory.dataSource.invalidate() method will make a refresh, alternatively instead of keeping dataSource instance inside the factory, we can call invalidate method on LiveData< PagedList < CouponModel > >.getValue().getDataSource().invalidate()

public class CouponListDataSourceFactory extends DataSource.Factory {

private CouponListDataSource dataSource;

private CouponRepository repository;
private String token;
private String vendorId;

public CouponListDataSourceFactory(CouponRepository repository, String token, String vendorId) {
    this.repository = repository;
    this.token = token;
    this.vendorId = vendorId;
}

@Override
public DataSource create() {
    dataSource = new CouponListDataSource(repository, token, vendorId);
    return dataSource;
}
}


回答2:

Add a method in ViewModel class

 public void refresh() {

    itemDataSourceFactory.getItemLiveDataSource().getValue().invalidate();
}

and from the Activity/Fragment you can use

 swipeRefreshLayout.setOnRefreshListener(() -> yourviewModel.refresh());

Hide the refresh layout when the reyclerView gets loaded

yourViewModel.itemPagedList.observe(this, allProposalModel -> {


        mAdapter.submitList(model);
        swipeRefreshLayout.setRefreshing(false); //here..


    });