Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How differentiate between user input and setText method in TextWatcher?

I'm using a RecyclerView that contains items containing a EditText each one.

I add a TextWatcher in this EditText in the ViewHolder constructor. This text watcher call: adapter.notifyItemChanged()

When bind views, I do:

edittext.setText("value");

So text changed event is fired, but I don't want that beacuse I get

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

I want that event is fired only when user changes the text manually. Is there a way to differentiate this events?

I could remove and add again the TextWatcher each time views are binded but I'd like find another better solution.

like image 596
Héctor Avatar asked Jul 03 '26 08:07

Héctor


1 Answers

Instead of setting the TextWatcher in the ViewHolder constructor, you can do that while binding the view, right after the setText() call, avoiding the problem you are experiencing.

So:

edittext.setText("value");
edittext.setTextWatcher(new TextWatcher(..

This way the TextWatcher will not immediately fire an event because it didn't yet exist when the initial value was set.

like image 133
Tim Avatar answered Jul 05 '26 22:07

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!