我正在尝试使用notifyDataSetChanged()更新recycleview的项目. 这是我在recycleview适配器中的onBindViewHolder()方法. @Overridepublic void onBindViewHolder(ViewHolder viewHolder, int position) { //checkbox view listener vie
这是我在recycleview适配器中的onBindViewHolder()方法.
@Override public void onBindViewHolder(ViewHolder viewHolder, int position) { //checkbox view listener viewHolder.getCheckbox().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //update list items notifyDataSetChanged(); } }); }
我选中一个复选框后,我想要做的是更新列表项.我得到了一个非法的例外:“当RecyclerView计算布局或滚动时,无法调用此方法”
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:1462) at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:2982) at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:7493) at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:4338) at com.app.myapp.screens.RecycleAdapter.onRowSelect(RecycleAdapter.java:111)
我也使用了notifyItemChanged(),同样的例外.任何秘密的更新方式,以通知适配器的东西发生了变化?
您应该将方法’setOnCheckedChangeListener()’移动到ViewHolder,它是适配器上的内部类.onBindViewHolder()不是初始化ViewHolder的方法.
此方法是刷新每个回收物品的步骤.
当您调用notifyDataSetChanged()时,onBindViewHolder()将被调用为每个项目的次数.
因此,如果您将notifyDataSetChanged()放入onCheckChanged()并在onBindViewHolder()中初始化checkBox,则由于循环方法调用,您将获得IllegalStateException.
点击复选框 – > onCheckedChanged() – > notifyDataSetChanged() – > onBindViewHolder() – >设置复选框 – > onChecked …
简单地说,你可以通过在Adapter中放一个标志来解决这个问题.
试试这个,
private boolean onBind; public ViewHolder(View itemView) { super(itemView); mCheckBox = (CheckBox) itemView.findViewById(R.id.checkboxId); mCheckBox.setOnCheckChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(!onBind) { // your process when checkBox changed // ... notifyDataSetChanged(); } } ... @Override public void onBindViewHolder(YourAdapter.ViewHolder viewHolder, int position) { // process other views // ... onBind = true; viewHolder.mCheckBox.setChecked(trueOrFalse); onBind = false; }