expandable-recycler-view icon indicating copy to clipboard operation
expandable-recycler-view copied to clipboard

Update ParentViewHolder when I interact with ChildViewHolder

Open isaiahcat opened this issue 8 years ago • 1 comments

For example, I have a counter for how many crimes I've completed on the ParentViewHolder like (2/8). When I check the checkbox for the child crimes, I want the counter to update to (3/8). So far, I tried this.

    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            challenge.setDone(isChecked);
            notifyDataSetChanged();
        }
    });

But the line notifyDataSetChanged(); sometimes causes an app crash with this error.

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling

Is there a way where I can just update the ParentViewHolder from the ChildViewHolder without having to refresh the data set?

isaiahcat avatar Nov 06 '16 14:11 isaiahcat

Sorry for taking so long to respond.

I think what is happening with the call to notifyDataSetChanged is that you are kind of getting into a recursive loop. User checks the box -> checkbox triggers notifyDataSetChanged -> onBind is called for that child, the checkbox is set to true or false -> this setting of true and false triggers onCheckedChanged -> second call to notifyDataSetChanged() where this crashes.

This SO is what led me to believe this is the case here: http://stackoverflow.com/questions/27070220/recycleview-notifydatasetchanged-illegalstateexception

Solutions are then:

  • To clear out the checked change listener before re-setting the check state in onBind
  • OR Implement the boolean gate as shown in the SO

Let me know if I'm wrong in my thought of the problem.

paul-turner avatar Feb 03 '17 19:02 paul-turner