Recyclerview DiffUtil在更新时将我的列表滚动到顶部

时间:2017-09-06 09:12:48

标签: java android android-recyclerview

我决定在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);
            }

0 个答案:

没有答案