您的位置:首页 > 其它

Recycleview 使用中的问题

2016-05-23 19:48 369 查看
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());
}
}
});


}`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: