【发布时间】:2017-11-21 12:33:53
【问题描述】:
我已经使用 RecyclerView 实现了一个列表,并且在滚动时它将执行 API 调用以获取下一组数据,这些数据将更新到列表中。我使用此链接中概述的方法做到了这一点:https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView
所以我遇到的问题是,当我向下滚动时,会加载并显示新数据,但列表会跳回到列表的最顶部/开头。有没有办法防止列表移回顶部,但留在新数据已加载的位置?
谢谢!
我的完整代码可以在:https://github.com/chao-li/MovieNight.git
列表按如下方式启动: // 为电影列表创建适配器
MovieAdapter adapter = new MovieAdapter(mMovieListViewData.getTotalMovieDataArray());
mRecyclerView.setAdapter(adapter);
// As this is recycler view, requires a layout manager
//RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
And upon loading the data, the new list is updated as follow:
// append the temporaryMovieArray to the end of TotalMovieDataArray
mMovieListViewData.setTotalMovieDataArray(
appendArray(mMovieListViewData.getTotalMovieDataArray(),
mMovieListViewData.getTemporaryMovieArray()));
// --> Notify the adapter of the new items made with `notifyItemRangeInserted()`
MovieAdapter adapter = new MovieAdapter(mMovieListViewData.getTotalMovieDataArray());
mRecyclerView.setAdapter(adapter);
adapter.notifyItemRangeInserted(mCurrentMovieDataLength,
mMovieListViewData.getTotalMovieDataArray().length);
// update the starting position of new datas
mCurrentMovieDataLength = mMovieListViewData.getTotalMovieDataArray().length;
【问题讨论】:
标签: java android listview android-recyclerview