Загрузка страницы подкачки после не вызывается - PullRequest
0 голосов
/ 17 мая 2019

Я использую библиотеку подкачки

Для первого списка данных он работает нормально, но после реализации его в другом фрагменте для другого списка он не вызывает 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

...