IllegalStateException:当RecyclerView正在计算布局或滚动

时间:2017-07-26 10:52:19

标签: android android-recyclerview illegalstateexception

我正在尝试从RecyclerView中移除某个项目,但我总是收到错误。

  

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

我正在使用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);
    }
}

2 个答案:

答案 0 :(得分:8)

正如阿巴斯所说的

  

当你在bg线程上调用notify时,通常会发生这种情况。所以只需将通知移动到ui线程

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

这也会起作用。

答案 1 :(得分:0)

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

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

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