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

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

问题描述

我正在尝试从recyclerview中删除我的商品,但是我总是遇到错误

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

java.lang.IllegalStateException:在以下情况下无法调用此方法: RecyclerView正在计算布局或滚动

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

这只是问题的workaround.

通常在您在background thread上调用notifyDataSetChanged()时发生.因此,只需将通知移至UI thread

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

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

您使用您的RecyclerView实例,并在post方法内将新的Runnable添加到消息队列中.可运行对象将在用户界面线程上运行.这是Android从后台访问UI线程的限制(例如,在将在后台线程中运行的方法内部). 有关更多信息,请根据需要在UI线程上运行它.

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.

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

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天全站免登陆