当 RecyclerView 正在计算布局或滚动时,尝试从 recyclerview 中删除项目时无法调用此方法 [英] Cannot call this method while RecyclerView is computing a layout or scrolling when try remove item from recyclerview

查看:29
本文介绍了当 RecyclerView 正在计算布局或滚动时,尝试从 recyclerview 中删除项目时无法调用此方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 recyclerview 中删除我的项目,但我总是收到错误

<块引用>

java.lang.IllegalStateException: 无法调用此方法,而RecyclerView 正在计算布局或滚动

我正在使用notify datasetchanged,我能解决这个问题吗?

public class AdapterIntransit extends RecyclerView.Adapter{私有上下文上下文;列表数据;公共 AdapterIntransit(上下文上下文,列表 数据){this.context = 上下文;this.data = 数据;}@覆盖公共 AdapterIntransit.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {查看 itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardintransit, parent, false);返回新的 ViewHolder(itemView);}@覆盖公共无效 onBindViewHolder(AdapterIntransit.ViewHolder 持有人,int 位置){if (data.get(position).getJml1() - data.get(position).getJml2() <= 0) {数据删除(位置);notifyItemRemoved(位置);notifyItemRangeChanged(position, getItemCount());notifyDataSetChanged();} 别的 {holder.kode.setText(data.get(position).getKode());holder.nama.setText(data.get(position).getNama());holder.jumlah.setText(String.valueOf(data.get(position).getJml1() - data.get(position).getJml2()));}}@覆盖公共 int getItemCount() {返回数据大小();}公共类 ViewHolder 扩展 RecyclerView.ViewHolder{TextView kode、nama、jumlah;公共 ViewHolder(查看 itemView){超级(项目视图);kode = (TextView)itemView.findViewById(R.id.kode);nama = (TextView)itemView.findViewById(R.id.nama);jumlah = (TextView)itemView.findViewById(R.id.jumlah);}}}

解决方案

以下答案对我有用

这只是问题的解决方法.

这通常发生在您在 后台线程 上调用 notifyDataSetChanged() 时.所以只需将通知移动到 UI 线程

recyclerView.post(new Runnable(){@覆盖公共无效运行(){myadapter.notifyDataSetChanged();}});

<块引用>

您使用您的 RecyclerView 实例,并在 post 方法中将一个新的 Runnable 添加到消息队列中.runnable 将在用户界面线程上运行.这是 Android 从后台访问 UI 线程的限制(例如,在将在后台线程中运行的方法内部).如果需要,您可以在 UI 线程上运行它.

如果需要,您可以在 UI 线程上运行更多

 runOnUiThread(new Runnable(){公共无效运行(){//UI 代码在这里}});

I am Trying to remove my item from recyclerview, but i always getting error

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

i am using notify datasetchanged, can i solve this?

public class AdapterIntransit extends RecyclerView.Adapter<AdapterIntransit.ViewHolder> {
    private Context context;
    List<DataIntransit> data;

    public AdapterIntransit(Context context, List<DataIntransit> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public AdapterIntransit.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardintransit, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(AdapterIntransit.ViewHolder holder, int position) {
        if (data.get(position).getJml1() - data.get(position).getJml2() <= 0) {
            data.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, getItemCount());
            notifyDataSetChanged();
        } else {
            holder.kode.setText(data.get(position).getKode());
            holder.nama.setText(data.get(position).getNama());
            holder.jumlah.setText(String.valueOf(data.get(position).getJml1() - data.get(position).getJml2()));
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView kode, nama, jumlah;
        public ViewHolder(View itemView) {
            super(itemView);
            kode = (TextView)itemView.findViewById(R.id.kode);
            nama = (TextView)itemView.findViewById(R.id.nama);
            jumlah = (TextView)itemView.findViewById(R.id.jumlah);

        }
    }
}

解决方案

Below answer worked for me

This is just workaround for the problem.

This usually occurs when you are calling notifyDataSetChanged() on the background thread. So just move notify to UI thread

recyclerView.post(new Runnable()
            {
              @Override
              public void run() {
                myadapter.notifyDataSetChanged();
              }
            });

You use your RecyclerView instance and inside the post method a new Runnable added to the message queue. The runnable will be run on the user interface thread. This is a limit for Android to access the UI thread from background (e.g. inside a method which will be run in a background thread). for more you run it on UI thread if you needed.

For more you can run it on UI thread, if you needed

 runOnUiThread(new Runnable(){
 public void run() {
      // UI code goes here
 }
 });

这篇关于当 RecyclerView 正在计算布局或滚动时,尝试从 recyclerview 中删除项目时无法调用此方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆