在Recyclerview的條目中監(jiān)聽(tīng)EditText內(nèi)容變化小槐,隨著EditText內(nèi)容變化更新適配器诸老。發(fā)現(xiàn)刷新數(shù)據(jù)之后EditText會(huì)失去焦點(diǎn)败明,也就是說(shuō)不能在EditText中重復(fù)輸入數(shù)據(jù)
解決辦法:
index設(shè)置為adapter中的成員變量,等于-1
? ?editText.setOnTouchListener(new View.OnTouchListener() {
????????public boolean onTouch(View view, MotionEvent event) {
????????// 在TOUCH的UP事件中忠荞,要保存當(dāng)前的行下標(biāo)樊卓,因?yàn)閺棾鲕涙I盤后壁畸,整個(gè)畫面會(huì)被重畫
? ? ? ? // 在getView方法的最后茅郎,要根據(jù)index和當(dāng)前的行下標(biāo)手動(dòng)為EditText設(shè)置焦點(diǎn)
? ? ? ? if (event.getAction() == MotionEvent.ACTION_UP) {
????????????index =position;
????????}
????????return false;
????????}
????});
????????editText.addTextChangedListener(new TextWatcher() {
????????????????@Override
? ? ????????public void beforeTextChanged(CharSequence s,int start,int count,int after) { }
????????????@Override
? ????????? public void onTextChanged(CharSequence s,int start,int before,int count) { }
????????????@Override
? ? ????????public void afterTextChanged(Editable s) {
????????????notifyDataSetChanged();????//在這里進(jìn)行數(shù)據(jù)刷新? 之后會(huì)發(fā)現(xiàn)焦點(diǎn)還在EditText上
????????????}
????????});
????????editText.clearFocus();
????????if (index != -1 &&index == position) {
????????????// 如果當(dāng)前的行下標(biāo)和點(diǎn)擊事件中保存的index一致,手動(dòng)為EditText設(shè)置焦點(diǎn)拾因。
? ? ????????editText.requestFocus();
????????}