【问题标题】:RecyclerView infinite scroll moves position to first itemRecyclerView 无限滚动将位置移动到第一项
【发布时间】: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


    【解决方案1】:

    第二次加载数据后,不要初始化您的适配器,而是使用您已经初始化的相同适配器对象并将其设为全局变量,并在您的 Adapter 类中编写以下方法:

    public void setData(MovieData[] movieDatas)
    {
        mMovieDatas = movieDatas;
        notifyDataSetChanged();
    }
    

    & 从您的MovieListActivity 调用此方法 那么您的列表将不会跳到顶部。

    【讨论】:

    • 非常感谢!这对我有用。我对 Android 编码很陌生,你能解释一下 notifyDataSetChanged(); 之间有什么区别吗?和 notifyItemRangeInserted(); ?
    • notifyDataSetChanged 通知适配器数据集已更改,但它不会指定数据集已更改的内容,它将强制适配器重新加载所有数据并重新布局所有可见视图。其中 notifyItemRangeInserted 指定更改了数据的位置和数量。您可以根据需要使用其中任何一个。更多信息阅读文档:developer.android.com/reference/android/support/v7/widget/…
    【解决方案2】:

    Recycler.Adapter 提供了很多东西来做到这一点。

    检查一下:

    notifyItemRangeInserted(int positionStart, int itemCount)

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多