Recycleview 使用中的问题

1.使用Recycleview notifyDataSetChanged() IllegalStateException
解决:public void onBindViewHolder(final ViewHolder holder, final int position) {
SwitchCompat mySwitch = (SwitchCompat) view.findViewById(R.id.switch);
//Set it to null to erase an existing listener from a recycled view.
mySwitch.setOnCheckedChangeListener(null);

 //Set the switch to how it previously was without triggering the listener.
   mySwitch.setChecked(savedSwitchState); //If the saved state was "true", then this                will trigger the infinite loop.

//Set the listener now.
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            data.delete(position);
            notifyItemRemoved(position);
            //This will call onBindViewHolder, but we can't do that when we are already in onBindViewHolder!
            notifyItemRangeChanged(position, data.size());
        }
    }
});

}`

你可能感兴趣的:(Recycleview 使用中的问题)