sir mordred
sir mordred

Reputation: 161

Scrolling when keyboard opened Multiple edittext in RecyclerView

ok guys

i'm developing an android app which requires lots of edittext fields(so i put them into recyclerview for better ui performance)

firstly i encountered the input values on edittext fields are not saved so scrolling caused the lose of values in edittext field

i solved the problem by adding textwatcher to edittext fields and saving input data but now im encountering another problem: just try the following steps in proper order:

1- tap to some edittext input field(soft keyboard open ups simultaneously)
2- now scroll the recyclerview to other edittext fields (so edittext which you entering value into it, disappears from screen)
3- and try to input some words into it by keyboard

after these steps edittext which you edited doesnt contain any value.

instead, other randomly choosen edittext gains the value which you entered

probably recyclerview's reusing mechanism is causing it, not sure

i dont want to disable scrolling when soft keyboard opened to fix this possible behaviour/issue so is there another way to solve this problem?

EDIT:

public class SampleAdapter extends RecyclerView.Adapter{
private static List<String> mEditTextValues = new ArrayList<>();

public SampleAdapter(List<String> dataSet){
     mEditTextValues = dataSet;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new CustomViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.edittext,parent,false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    CustomViewHolder viewHolder = ((CustomViewHolder)holder);
    viewHolder.mEditText.setTag(position);
    viewHolder.mEditText.setText(mEditTextValues.get(position));
}
@Override
public int getItemCount() {
    return mEditTextValues.size();
}

public class CustomViewHolder extends RecyclerView.ViewHolder{
    private EditText mEditText;
    public CustomViewHolder(View itemView) {
        super(itemView);
        mEditText = (EditText)itemView.findViewById(R.id.editText);
        mEditText.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            public void afterTextChanged(Editable editable) {
                if(mEditText.getTag()!=null){
                    mEditTextValues.set((int)mEditText.getTag(),editable.toString());
                }
            }
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
        });
    }
}

}

Upvotes: 0

Views: 1417

Answers (3)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

If you want to stop editing random place, you can hide the soft keypad when the current editing edit text starts non-focusing.

Adapter.bindView

viewHolder.mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b == false) {
                    hideKeyboard(view.getContext(), view);
                }
            }
        });

To hide the keypad

/**
 * To hide a keypad.
 *
 * @param context
 * @param view
 */
public static void hideKeyboard(Context context, View view) {
    if ((context == null) || (view == null)) {
        return;
    }
    InputMethodManager mgr =
            (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Upvotes: 1

Patrick R
Patrick R

Reputation: 6857

Try to put your TextWatcher in onBindViewHolder it will work

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    CustomViewHolder viewHolder = ((CustomViewHolder)holder);

    viewHolder.mEditText.setText(mEditTextValues.get(position));

viewHolder.mEditText.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            public void afterTextChanged(Editable editable) {

            }
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
               mEditTextValues.set(position,mEditText.getText().toString())
}
        });
}

Upvotes: 0

CCT
CCT

Reputation: 70

It sounds like you need a ScrollView in your layout but there is no code posted

Upvotes: 0

Related Questions