pchmn / MaterialChipsInput

Implementation of Material Design Chips component for Android
Apache License 2.0
2.63k stars 382 forks source link

java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{... #61

Open FranPR9 opened 7 years ago

FranPR9 commented 7 years ago

I get this: java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{15b78fb4 position=4 id=-1, oldPos=-1, pLpos:-1 not recyclable(0)} whenever I try to: chipsInput.addChip(contact); of course contact class implements ChipInterface sometimes it do work, most of the time it gives me this error

mathias21 commented 7 years ago

Same here. In my case I still didn't see it working. I will investigate a bit, let's see if I find something.

mathias21 commented 7 years ago

Hi @FranPR9

I've fixed this by adding a method to ChipsInput class to add a list of chips. Then I forward it to the adapter and in the adapter it looks like this:

public void addChipsProgrammatically(List<ChipInterface> chipList){
        if(chipList != null){
            if(chipList.size() > 0) {
                int chipsBeforeAdding = getItemCount();
                for (ChipInterface chip : chipList) {
                    mChipList.add(chip);
                    mChipsInput.onChipAdded(chip, getItemCount());
                }

                // hide hint
                mEditText.setHint(null);
                // reset text
                mEditText.setText(null);

                notifyItemRangeChanged(chipsBeforeAdding, chipList.size());
            }
        }
    }

Most likely, the problem comes from notifying recyclerview about item added just after setting adapter. Could that be correct?

I hope this helps, maybe soon I will make another PR.

FranPR9 commented 6 years ago

I didn't try your solution, but for me it worked to make the chipsinput visible until it has chips in its list. I think the problem has to do with the recycler being notified at a certain time for it to be resized. thanks @mathias21

sameerjj commented 6 years ago

solved this in a similar way by setting a minHeight in XML. stops the initial resizing to be messy I assume.

hamedhoseini commented 5 years ago

I have the same problem and tried all of the above solutions but it has existed. Does anybody have other idea?