键盘打开时滚动RecyclerView中的多个edittext

时间:2017-06-17 02:02:39

标签: java android android-edittext android-recyclerview

好人

我正在开发一个需要大量edittext字段的Android应用程序(因此我将它们放入recyclerview以获得更好的ui性能)

首先我遇到edittext字段的输入值没有保存,因此滚动导致edittext字段中的值丢失

我通过将textwatcher添加到edittext字段并保存输入数据来解决了这个问题,但现在我遇到了另一个问题: 只需按正确的顺序尝试以下步骤:

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

经过这些步骤 您编辑的edittext不包含任何值。

相反,其他随机选择的edittext获得您输入的值

可能是recyclerview的重用机制导致它,不确定

我不想在打开软键盘时禁用滚动来修复这个可能的行为/问题所以有另一种方法可以解决这个问题吗?

编辑:

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) {}
        });
    }
}

}

3 个答案:

答案 0 :(得分:1)

如果要停止编辑随机位置,可以在当前编辑编辑文本开始非聚焦时隐藏软键盘。

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 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);
}

答案 1 :(得分:0)

听起来您需要在布局中使用ScrollView,但没有发布代码

答案 2 :(得分:0)

尝试将TextWatcher放入onBindViewHolder中,它将起作用

@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())
}
        });
}