Android RecyclerView : notifyDataSetChanged() IllegalStateException

前端 未结 22 1861
天涯浪人
天涯浪人 2020-11-28 02:57

I\'m trying to update the items of a recycleview using notifyDataSetChanged().

This is my onBindViewHolder() method in the recycleview adapter.

@Over         


        
22条回答
  •  醉酒成梦
    2020-11-28 03:35

    Why not checking the RecyclerView.isComputingLayout() state as follows?

    public class MyAdapter extends RecyclerView.Adapter{
    
        private RecyclerView mRecyclerView; 
    
        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
            mRecyclerView = recyclerView;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
    
            viewHolder.getCheckbox().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (mRecyclerView != null && !mRecyclerView.isComputingLayout()) {
                        notifyDataSetChanged();
                    }
                }
            });
        }
    }
    

提交回复
热议问题