As we all know, when using a ListView, when there is a large amount of data will be used in the reuse of the wording, and Recyclerview has a mechanism for reuse. And because of the reuse relationship, if the item layout involves some controls such as EditText, there will be data confusion caused by the reuse.
Especially when the item setting data, if your edittext has joined the Textwatcher event listener, and has the operation in aftertextchanged to the text, at this time, you can't set the text for EditText, because a set of text, The Aftertextchanged method is triggered, and then a series of problems occur.
The solution is to determine whether EditText has joined the Textwatcher event before joining the Textwatcher event, remove it first, and then add the Textwatcher event to the EditText after setting the text. Here can be handled by tag tag, the implementation method is as follows:
(Java notation)
Determine if there is a Textwatcher listener event, and some words first remove if (Holder.editText.getTag (r.id.search_key) instanceof Textwatcher) {
Holder.editText.removeTextChangedListener ((Textwatcher) Holder.editText.getTag (R.id.search_key));
}//Remove the Textwatcher event after setting the item corresponding to the text Holder.editText.setText ("new text"); Textwatcher textwatcher=new Textwatcher () {@Override public void beforetextchanged (charse quence s, int start, int count, int after) {} @Override public void OnT
Extchanged (charsequence s, int start, int before, int count) {} @Override
public void aftertextchanged (Editable s) {beans.get (i). SetValue (S.tostring ());
}
};
Set tag to Textwatcher Holder.editText.addTextChangedListener (Textwatcher); Holder.editText.setTag (R.id.search_key,textwatcher);
(Kotlin notation)
if (Edittext.gettag (r.id.item_add_inputs) is Textwatcher) {
Edittext.removetextchangedlistener (editText.getTag (r.id.item_add_inputs) as Textwatcher)
}
edittext.settext (data.content)
var textwatcher:textwatcher = object:textwatcher{
Override Fun aftertextchanged (s:editable?) {
data.content = s.tostring ()
}
Override fun Beforetextchanged (s:charsequence?, Start:int, Count:int, After:int) {
}
override Fun OnTextChanged (S:charsequence, Start:int, Before:int, Count:int) {
}
}< C12/>edittext.addtextchangedlistener (Textwatcher)
Edittext.settag (R.id.item_add_inputs,textwatcher)
I've had this problem for the second time, so make a note.
qq:361561789
Can directly add Q contact