gpt4 book ai didi

android - RecyclerView notifyItemInserted IllegalStateException

转载 作者:IT老高 更新时间:2023-10-28 21:53:57 27 4
gpt4 key购买 nike

我正在将我的适配器移植到 RecyclerView.Adapter

我想要达到的目标:当用户在接近尾声时向下滚动时,我想开始获取数据,我还想在最后添加我的 ProgressBar View ,让用户知道更多数据即将到来。

我在 BaseAdapter 中实现这一点的方式: 在接近结束时请求的 View 中的 getView 上,我将开始获取更多数据,调用 notifyDataSetChanged(获取要显示的 ProgressBar View ),然后才返回 getView 所需的 View 。

我在 RecyclerView.Adapter 中尝试做的事情: 我基本上尝试做同样的事情,这次是在 onBindViewHolder 方法中,

但如果我尝试在此方法中调用 notifyItemInserted 我会得到以下异常:

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

我尝试了什么: 我注意到 onBindViewHolderonLayoutChildrenLayoutManager 调用,我尝试覆盖它并在其 super 之后调用 notifyItemInserted 但我得到了同样的异常

我怎样才能实现我的目标?

最佳答案

        Handler handler = new Handler();

final Runnable r = new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
};

handler.post(r);

我调用adapter.notifyDataSetChanged()的示例代码;来自 Activity

关于android - RecyclerView notifyItemInserted IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26555428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com