我正在使用一个 RecyclerView
,其中包含每个项目都包含一个 EditText
。
我在 ViewHolder 构造函数的这个 EditText 中添加了一个 TextWatcher
。此文本观察器调用:adapter.notifyItemChanged()
绑定(bind) View 时,我会:
edittext.setText("value");
所以文本更改事件被触发,但我不想要那个,因为我得到了
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
我希望只有当用户手动更改文本时才会触发该事件。有没有办法区分这些事件?
每次绑定(bind) View 时,我都可以删除并再次添加 TextWatcher
,但我想找到另一个更好的解决方案。
最佳答案
无需在 ViewHolder 构造函数中设置 TextWatcher,您可以在调用 setText() 之后绑定(bind) View 时执行此操作,从而避免您遇到的问题。
所以:
edittext.setText("value");
edittext.setTextWatcher(new TextWatcher(..
这样 TextWatcher 就不会立即触发事件,因为在设置初始值时它还不存在。
关于android - 如何区分 TextWatcher 中的用户输入和 setText 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33371722/