RecyclerView中嵌套Edittext出现的数据错乱问题解决

RecyclerView 一般用于数据的展示,当我们需要从服务器拿到数据,对数据进行增删改后上传给我们的服务器,如果你的RecyclerView 的Item如下图中包含Edittext时,问题就来了,出现各种Edittext数据错乱问题.


item样式.jpg

下面直接上解决办法:
处理RecyclerView的Adapter,在ViewHolder中绑定Edittext,并在onBindViewHolder()中通过在适当的时机添加或移除Edittext的TextChangedListener来处理数据错乱的问题.这个适当的时机===>选在Edittext获得焦点的时候,添加监听器,失去焦点的时候再移除监听器,可以保证数据的正确性,然后通过在监听器里添加接口回调将数据传递到Activity中,修改Activity中list里的bean,即可达到编辑的目的

public class RvCarScarAdapter extends RecyclerView.Adapter {
private Context mContext;
private List mList;
private onTextChangeListener mTextListener;
public RvCarScarAdapter(Context context, List list) {
    mContext = context;
    mList = list;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.item_car_scar, parent, false);
    return new ItemHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
    final ItemHolder itemHolder = (ItemHolder) holder;
      itemHolder.etContent.setText(mList.get(position).getCarScarRecordContent());
  
    final TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence sequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence sequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //通过接口回调将数据传递到Activity中,修改list里的bean
            if (itemHolder.etContent.hasFocus()) {//如果获得焦点再去回调
                mTextListener.onTextChanged(position, itemHolder.etContent.getText().toString());
            }

        }
    };
    //设置EditText的焦点监听器判断焦点变化,当有焦点时addTextChangedListener,失去焦点时removeTextChangedListener
    itemHolder.etContent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if(hasFocus){
                itemHolder.etContent.addTextChangedListener(watcher);
            }else {
                itemHolder.etContent.removeTextChangedListener(watcher);
            }
        }
    });


}

@Override
public int getItemCount() {
    if(mList!=null &&mList.size()>0){
        return mList.size();
    }
    return 0;
}
public interface onTextChangeListener {
    void onTextChanged(int pos,String str);
}
//设置Edit监听
public void setOnTextChangeListener(onTextChangeListener onTextChangeListener){
    this.mTextListener=onTextChangeListener;
}

public void setData(List data) {
    mList = data;
    notifyDataSetChanged();
}


class ItemHolder extends RecyclerView.ViewHolder{
    EditText etContent;
    public ItemHolder(View itemView) {
        super(itemView);
        etContent= itemView.findViewById(R.id.et_content);
    }
}

在Acitvity中设置接口回调

   mScarAdapter.setOnTextChangeListener(new RvCarScarAdapter.onTextChangeListener() {
        @Override
        public void onTextChanged(int pos, String str) {
            mScarBeanList.get(pos).setCarScarRecordContent(str);//修改对应position item编辑的数值
        }
    });

你可能感兴趣的:(RecyclerView中嵌套Edittext出现的数据错乱问题解决)