Workaround for a ListView or Recyclerview nested edittext with a reuse problem

Source: Internet
Author: User
Tags event listener

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.