Cannot call notifyItemInserted() from RecyclerView.OnScrollListener

拟墨画扇 提交于 2019-12-02 17:25:20

You could also post using the view.

   recyclerView.post(new Runnable() {
        public void run() {
            articleAdapter.notifyItemInserted(allArticles.size() - 1);
        }
    });
  1. The issue is not with new version of Recyclerview.

2 & 3. You cannot change item while it is setting (with calling onBindViewHolder). In that case you have to call notifyItemInserted at the end of current loop by calling Handler.post()

Handler handler = new Handler();

    final Runnable r = new Runnable() {
        public void run() {
            articleAdapter.notifyItemInserted(allArticles.size() - 1);
        }
    };

    handler.post(r);

I hope, it will solve your problem.

You could also use Google Play Service's Tasks API

Tasks.call(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
        allArticles.add(null);
        articleAdapter.notifyItemInserted(allArticles.size() - 1);
        return null;
    }
});

In some cases onScrollStateChanged may be enough

override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
    super.onScrollStateChanged(recyclerView, newState)
    adapter.notifyDataSetChanged()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!