如何使用新的分页库实现SwipeRefreshLayout [英] How to implement SwipeRefreshLayout with the new Paging Library

查看:209
本文介绍了如何使用新的分页库实现SwipeRefreshLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,向用户显示项目列表,它使用了分页库.我的问题是,当用户向下滑动屏幕以使其再次从服务器获取数据时,我无法重新加载列表.

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.

这是我的数据源工厂:

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;
    }
}

这是我创建PagedList的方法

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();

推荐答案

调用 mDataSource.invalidate()方法后,mDataSource将失效,并且将通过DataSource.Factory创建新的DataSource实例. create()方法,因此对每次每次在DataSource.Factory.create()方法中提供新的DataSource()实例都很重要,不要每次都提供相同的DataSource实例.

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()无法正常工作,因为失效后,CouponListDataSourceFactory提供了相同的,已经失效的DataSource实例.

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

修改后,CouponListDataSourceFactory将看起来像下面,并调用 mCouponListDataSourceFactory.dataSource.invalidate()方法将刷新,或者替代,而不是保留dataSource实例在工厂内部,我们可以在 LiveData<分页列表< CouponModel>> .getValue().getDataSource().invalidate()

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;
}
}

这篇关于如何使用新的分页库实现SwipeRefreshLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆