IllegalStateException:RecyclerView 正在计算布局或滚动时无法调用此方法

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

我正在尝试从我的 RecyclerView 中删除一个项目,但我总是遇到错误。

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

我正在使用 notifyDataSetChanged()。我该如何解决?

这是我的适配器代码

public class ListAdapters extends RecyclerView.Adapter<ListAdapters.MyViewHolder> {

    public ArrayList<String> tvdatalist;
    Context c;
    int pos;
    ListAdapters.MyViewHolder myViewHolder;
    private LayoutInflater layoutInflater;
    int[] arr;

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        public EditText edttxt;
        public CheckBox cb;

        public MyViewHolder(View view) {
            super(view);

            edttxt = (EditText) view.findViewById(R.id.edttxt);
            cb = (CheckBox) view.findViewById(R.id.cb);
        }

        @Override
        public void onClick(View v) {

        }


    }

    public ListAdapters(Context c, ArrayList<String> tvdatalist) {
        this.c = c;
        this.layoutInflater = LayoutInflater.from(c);
        this.tvdatalist = tvdatalist;
        arr = new int[tvdatalist.size()];
        for (int i = 0; i < 20; i++) {
            arr[i] = 0;

        }


    }

    @Override
    public ListAdapters.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListAdapters.MyViewHolder(this.layoutInflater.inflate(R.layout.list_ittm, parent, false));
        //return new LandingPageAdapter.MyViewHolder(itemView);
    }

    public void onBindViewHolder(final ListAdapters.MyViewHolder holder, final int position) {
        myViewHolder = holder;

        final String ShowsBean = tvdatalist.get(position);
        myViewHolder.edttxt.setText(ShowsBean);
        if (arr[pos] == 0) {
            myViewHolder.cb.setChecked(false);
            myViewHolder.edttxt.setKeyListener(null);
        } else {
            myViewHolder.cb.setChecked(true);
            myViewHolder.edttxt.setFocusable(true);
        }



        myViewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    if (arr[position]==0){
                        arr[position]=1;
                    }else{
                        arr[position]=0;

                    }
                 notifyDataSetChanged();

            }

        });

    }
    @Override
    public int getItemCount() {
        return tvdatalist.size();

    }

    public interface ItemClickListener {
        void onClick(View view, int position, boolean isLongClick);
    }
}

正如阿巴斯所说

This usually occurs when you are calling notify on bg thread. So just move notify to ui thread

否则你可以使用notifyItemChanged(position);

这也可以。

当您尝试更新 recyclerview 时它仍然使项目膨胀时出现此问题,请尝试将 notifyItemChanged(position) 移动到您确定项目已添加到视图的位置,然后再调用 - 方法(notifyItemChanged(位置));

您还可以使用 runnable 在 UI 线程上进行更改。

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

这将通知 UI 线程上的适配器更改。