我已经使用RecyclerView实现了一个List,并且在滚动时它将执行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;
答案 0 :(得分:1)
第二次加载数据后,不要初始化适配器,而不是使用已经初始化的相同适配器对象。使它成为全局变量,&在Adapter
课程中写下以下方法:
public void setData(MovieData[] movieDatas)
{
mMovieDatas = movieDatas;
notifyDataSetChanged();
}
&安培;从MovieListActivity
调用此方法
然后你的列表不会跳到顶部。
答案 1 :(得分:0)