Bartos
Bartos

Reputation: 1037

Android notify adapter in TextWatcher

In my app I have RecyclerView with Edittext in each row. I'm using text watcher to get text from user and set correct value in all rows (depends on users input). Problem is, that to make list refreshes after each input I need to call notifyDataSetChanged(). This cause issue that after putting first number in edittext ,adapter is being notified and edittext loses focus so basically user can enter only one input. Do you have any idea how to deal with this problem ? Here's my code :

    public TextWatcher getUsernameData() {
            return 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) {
                    // Here I'm doing calculations but after first input edit text 
loses focus because of notifying adapter

                }
                @Override
                public void afterTextChanged(Editable s) {

                }
            };
        }

As you can see I'm doing calculation in onTextChanged() method.

Upvotes: 1

Views: 363

Answers (1)

User One
User One

Reputation: 465

You can try to keep a reference to the current View where you have focus,

Then in onBindViewHolder() you can identify the View you was typing text and request focus again to that View Like this :

yourEditText.requestFocus();
InputMethodManager inputMethodManager = 
 (InputMethodManager) 
  getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(yourEditText, 
InputMethodManager.SHOW_IMPLICIT);

Or you can also keep a reference to all View in you adapter when set, then instead of using notifyDataSetChanged() you could loop each View and set the new Data,

But you should review your implementation, i don't know what you are trying to achieve look like similar to a filter/search

Upvotes: 2

Related Questions