未调用后分页库加载

时间:2019-05-17 16:39:02

标签: android android-recyclerview android-architecture-components android-paging-library

我正在使用分页库

对于第一个数据列表,它可以正常工作,但是在为其他列表的另一个片段中实现它之后,它不调用loadAfter,而仅调用loadInitial

这是我的代码:

  

数据源

public class FavGrpDataSource extends PageKeyedDataSource<Long, ItemFavoriteGroup> {
private MutableLiveData networkState;
private MutableLiveData initialLoading;
private FavoriteRestApi favoriteRestApi;

public FavGrpDataSource(){
    networkState = new MutableLiveData();
    initialLoading = new MutableLiveData();
    favoriteRestApi = new FavoriteRestApi();
}

public MutableLiveData getNetworkState() {
    return networkState;
}

public MutableLiveData getInitialLoading() {
    return initialLoading;
}

@Override
public void loadInitial(@NonNull LoadInitialParams<Long> params, @NonNull LoadInitialCallback<Long, ItemFavoriteGroup> callback) {
    Log.e("FavDataSource", "Load initial ");
    initialLoading.postValue(NetworkState.LOADING);
    networkState.postValue(NetworkState.LOADING);

    ArrayList result = favoriteRestApi.getFavoriteGroups(1l);
    callback.onResult(result,null,2l);
    initialLoading.postValue(NetworkState.LOADED);
    networkState.postValue(NetworkState.LOADED);
}

@Override
public void loadBefore(@NonNull LoadParams<Long> params, @NonNull LoadCallback<Long, ItemFavoriteGroup> callback) {
}

@Override
public void loadAfter(@NonNull LoadParams<Long> params, @NonNull LoadCallback<Long, ItemFavoriteGroup> callback) {
    Log.e("FavDataSource", "Loading Rang " + params.key + " Count " + params.requestedLoadSize);
    networkState.postValue(NetworkState.LOADING);
    ArrayList result = favoriteRestApi.getFavoriteGroups(params.key);
    long nextKey = params.key+1;
    networkState.postValue(NetworkState.LOADED);
    callback.onResult(result,nextKey);
}
}
  

数据源工厂

public class DataFavGrpFactory extends DataSource.Factory {

private MutableLiveData<FavGrpDataSource> mutableLiveData;
private FavGrpDataSource feedDataSource;

public DataFavGrpFactory(){
    this.mutableLiveData = new MutableLiveData<FavGrpDataSource>();
}
@Override
public DataSource create() {
    feedDataSource = new FavGrpDataSource();
    mutableLiveData.postValue(feedDataSource);
    return feedDataSource;
}

public MutableLiveData<FavGrpDataSource> getMutableLiveData() {
    return mutableLiveData;
}
}
  

PagedListAdapter

public class FavGrpListAdapter extends PagedListAdapter<ItemFavoriteGroup, RecyclerView.ViewHolder> {
private static final int TYPE_PROGRESS = 0;
private static final int TYPE_ITEM = 1;

private Context context;
private NetworkState networkState;

public FavGrpListAdapter(Context context){
    super(ItemFavoriteGroup.DIFF_CALLBACK);
    this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    final ItemFavortieBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.item_favortie,parent,false);

    return new MyViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    LayoutInflater inflater = LayoutInflater.from(context);
    if (holder instanceof MyViewHolder){
        MyViewHolder myViewHolder = (MyViewHolder)holder;
        Log.e("onBindViewHolder",position+"..");
    }
}

class MyViewHolder extends RecyclerView.ViewHolder{
    ItemFavortieBinding binding;
    ItemFavoriteGroup itemFavoriteGroup;
    public MyViewHolder(@NonNull ItemFavortieBinding itemView) {
        super(itemView.getRoot());
        binding = itemView;
    }
    public void bindTo(ItemFavoriteGroup itemFavoriteGroup){
        this.itemFavoriteGroup = itemFavoriteGroup;
        binding.setModel(itemFavoriteGroup);
    }
}
}
  

ViewModel

public class FavoriteViewModel extends ViewModel {
private LiveData<NetworkState> networkState;
private LiveData<PagedList<ItemFavoriteGroup>> articleLiveData;
private Executor executor;
private DataFavGrpFactory dataFactory;

public FavoriteViewModel(DataFavGrpFactory dataFactory){
    this.dataFactory = dataFactory;
    init();
}

private void init(){
    executor = Executors.newFixedThreadPool(5);

    networkState = Transformations.switchMap(dataFactory.getMutableLiveData(),
            new Function<FavGrpDataSource, LiveData<NetworkState>>() {
                @Override
                public LiveData<NetworkState> apply(FavGrpDataSource input) {
                    return input.getNetworkState();
                }
            });
    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setInitialLoadSizeHint(10)
                    .setPageSize(20).build();
    articleLiveData = (new LivePagedListBuilder(dataFactory,pagedListConfig))
            .setFetchExecutor(executor)
            .build();
}
public LiveData<PagedList<ItemFavoriteGroup>> getArticleLiveData() {
    return articleLiveData;
}
}
  

片段

public class FavoriteFragment extends Fragment {

private FavoriteViewModel mViewModel;
private FavoriteFragmentBinding binding;
private FavGrpListAdapter adapter;

public static FavoriteFragment newInstance() {
    return new FavoriteFragment();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    binding = FavoriteFragmentBinding.inflate(inflater,container,false);
    return binding.getRoot();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    DataFavGrpFactory dataFactory = new DataFavGrpFactory();
    mViewModel = ViewModelProviders.of(this,new ViewModelFactory(dataFactory))
            .get(FavoriteViewModel.class);
    // TODO: Use the ViewModel
    binding.setLifecycleOwner(this);
    binding.recyclerview.setLayoutManager(new GridLayoutManager(getContext(),2));
    adapter = new FavGrpListAdapter(getContext());
    binding.recyclerview.setAdapter(adapter);

    mViewModel.getArticleLiveData().observe(this, new Observer<PagedList<ItemFavoriteGroup>>() {
        @Override
        public void onChanged(PagedList<ItemFavoriteGroup> itemAds) {
            adapter.submitList(itemAds);
        }
    });

}
  

片段XML文件

enter image description here

0 个答案:

没有答案