How Do I Use setOnFocusChangeListener with RecyclerView?

和自甴很熟 提交于 2020-07-08 11:15:15

问题


I have the following on onBindViewHolder() in Adapter Class for RecyclerView:

holder.answerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(!hasFocus){

                        String answer = holder.answerEditText.getText().toString();

                        mDatasetAnswers.add(answer);

                    }
                }
            });

The above only returns input from the first editText in the recyclerview. What could I be doing wrong? I would like it to return text from all EditTexts in the recyclerview.


回答1:


This happens because of the keyboard which pops up once you click on any Edit box in a recyclerview because onbindview is called and the focus changes to the first box in the recyclerview as all rows are reinflated again.

Hence, monitor for on focus gain and do ur stuff first before keyboard pops up. Hope this helps.




回答2:


Try to add these lines:

holder.answerEditText.setFocusable(true);
holder.answerEditText.setFocusableInTouchMode(true);

With these lines you make sure that the component can capture the focus.



来源:https://stackoverflow.com/questions/47242007/how-do-i-use-setonfocuschangelistener-with-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!