我决定在DiffUtil
中尝试notifyDataSetChanged
而不是EndlessRecyclerView
。
问题是,在第一次或第二次投掷后,我的list
会滚动到顶部,但这些项目会添加到列表中。
这是我的DiffUtil
:
public class MovieDiffCallback extends DiffUtil.Callback {
List<Movie> mOldMovieList;
List<Movie> mNewMovieList;
public MovieDiffCallback(List<Movie> oldMovieList, List<Movie> newMovieList) {
this.mOldMovieList = oldMovieList;
this.mNewMovieList = newMovieList;
}
@Override
public int getOldListSize() {
return mOldMovieList != null ? mOldMovieList.size() : 0;
}
@Override
public int getNewListSize() {
return mNewMovieList != null ? mNewMovieList.size() : 0;
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mOldMovieList.get(oldItemPosition).getId().equals(mNewMovieList.get(newItemPosition).getId());
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return mOldMovieList.get(oldItemPosition).equals(mNewMovieList.get(newItemPosition));
}
@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
return super.getChangePayload(oldItemPosition, newItemPosition);
}
}
这就是我在Fragment
中使用它的地方:
@Override
public void getMovies(List<Movie> moviesList) {
mDiffCallback = new MovieDiffCallback(mMoviesList, moviesList);
mDiffResult = DiffUtil.calculateDiff(mDiffCallback);
mMoviesList.addAll(moviesList);
mDiffResult.dispatchUpdatesTo(mAdapter);
isLoadedLandscape = true;
}
这是在EndlessRecyclerScroll
:
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
scrollPage++;
populateMap(scrollPage);
mPresenter.getSuggestedMovies(searchParamsMap);
}