I want to be able to add items to my ReclycerView dynamically.
When an item loads -> setText() -> I add another item on list.
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Message message = mDataset.get(position);
if(message.isAnswers()) {
holder.mAnswer1Button.setText(message.getAnswer1());
holder.mAnswer2Button.setText(message.getAnswer2());
holder.mAnswer1Button.setOnClickListener(v -> {
if(message.getChild1() > 0) {
add(position + 1, dataListShared.get(message.getChild1()));
holder.mAnswer1Button.setClickable(false);
holder.mAnswer2Button.setEnabled(false);
}
});
} else {
holder.mMessageTextView.setText(message.getMessage());
if(message.getChild1() > 0) {
add(position + 1, dataListShared.get(message.getChild1()));
holder.mMessageTextView.setEnabled(false);
}
}
}
This is what I have inside onBindViewHolder
. When I am on the first case if()
, and I click the button, the item is added to the list. On the second Case else()
, I would like for the text to be set on this current item and than already add another one.
How can I achieve this?
Moreover, why add()
works inside onClickListener but not outside of it?
The error I get is:
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
Thanks! :)
onBindViewHolder
. Thats exactly my question, where would that be? How do I know when an Item was added, and than react to it? – Lucas Storti Jan 22 '17 at 07:35