RecyclerView notifyDataSetChanged IllegalStateException

时间:2016-03-28 18:21:43

标签: android android-recyclerview android-adapter

我创建了一个使用RecyclerView的移动应用程序,每次从dataBase 10项加载分页,然后当列表到达底部时我加载了10个其他项,所以我使用这个方法,如果我到达结束时通知列表:

public boolean reachedEndOfList(int position) {
        // can check if close or exactly at the end
        return position == getItemCount() - 1;
    }

我使用此功能加载项目:

 @Override
    public void onBindViewHolder(Info holder, int position, List<Object> payloads) {
        if (reachedEndOfList(position)) {
            Log.d("reachedEnd", "true");
            this.autoCompletesTmp = getTmp();
            notifyDataSetChanged();
        }
    }

getTmp()使用另外10个项目更新项目列表,但是当我到达列表底部时出现此异常:

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling

1 个答案:

答案 0 :(得分:1)

前一段时间我遇到了同样的问题: 这有助于:

Handler handler = new Handler();
                final Runnable r = new Runnable() {
                    public void run() {

                        //Do something like notifyDataSetChanged()
                    }
                };

                handler.post(r);