RecyclerView 获取条目中EditText的值, 增加条目和删除条目

场景描述:
RecyclerView的条目中包含三个EditText,点击增加即新增一个条目, 点击删除删除某一固定位置的条目

实现思路:
每个输入框的内容在点击增加的时候需要保存到各自的List集合中, 再重新刷新UI. 保存数据的方法, 在EditText的addTextWatcher的监听方法里边进行保存

具体适配器的代码如下

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{private Context context;private List<String> brands;private List<String> types;private List<String> nums;public Adapter(Activity context) {this.context = context;brands = new ArrayList<>();types = new ArrayList<>();nums = new ArrayList<>();brands.add( "" );types.add( "" );nums.add( "" );}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(context).inflate(R.layout.item_recycler,parent,false);ViewHolder holder = new ViewHolder(view);return holder;}@Overridepublic void onBindViewHolder( final ViewHolder holder, final int position) {if( holder.amountEt.getTag() instanceof TextWatcher){holder.amountEt.removeTextChangedListener(((TextWatcher) holder.amountEt.getTag()));}if( holder.typeEt.getTag() instanceof TextWatcher){holder.typeEt.removeTextChangedListener(((TextWatcher) holder.typeEt.getTag()));}if( holder.brandEt.getTag() instanceof TextWatcher){holder.brandEt.removeTextChangedListener(((TextWatcher) holder.brandEt.getTag()));}holder.amountEt.setText( nums.get(position) );holder.brandEt.setText( brands.get(position) );holder.typeEt.setText( types.get(position) );if( position == brands.size() - 1){holder.addTxt.setVisibility( View.VISIBLE );}else {holder.addTxt.setVisibility( View.GONE );}holder.deleteTv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {brands.remove( position );types.remove( position );nums.remove( position );notifyDataSetChanged();notifyItemRemoved( position );}});holder.addTxt.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {brands.add( position+1, "" );types.add( position+1, "" );nums.add( position+1, "" );notifyDataSetChanged();notifyItemRemoved( position+1 );}});TextWatcher brandWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {if( brands.get( position ) != null){brands.remove( position );}brands.add( position, s.toString() );}};holder.brandEt.addTextChangedListener( brandWatcher );holder.brandEt.setTag( brandWatcher );TextWatcher typeWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {if( types.get( position ) != null){types.remove( position );}types.add( position, holder.typeEt.getText().toString() );}};holder.typeEt.addTextChangedListener( typeWatcher );holder.typeEt.setTag( typeWatcher );TextWatcher amountWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {if( nums.get( position ) != null ){nums.remove( position );}nums.add( position, holder.amountEt.getText().toString() );}};holder.amountEt.addTextChangedListener( amountWatcher );holder.amountEt.setTag( amountWatcher );}@Overridepublic int getItemCount() {return brands == null ? 0 : brands.size();}class ViewHolder extends RecyclerView.ViewHolder {private EditText brandEt;private EditText typeEt;private EditText amountEt;private LinearLayout llMain;private TextView deleteTv, addTxt;public ViewHolder(View itemView) {super(itemView);brandEt = (EditText) itemView.findViewById(R.id.item_recycler_equipment_brand_et);typeEt = (EditText) itemView.findViewById(R.id.item_recycler_equipment_type_et);amountEt = (EditText) itemView.findViewById(R.id.item_recycler_equipment_amount_et);deleteTv = (TextView) itemView.findViewById(R.id.item_recycler_delete_tv);addTxt = (TextView) itemView.findViewById(R.id.addEdd);llMain = (LinearLayout) itemView.findViewById(R.id.item_recycler_ll_main);}}
}

需要注意的是:
每次调用 notifyDataSetChanged(); 后, 会重新走editText的addTextChangedListener 方法, 造成集合中数据重复. 解决方案就是设置tag, 移除editText的 addTextChangedListener ,就可以了

本文链接:https://my.lmcjl.com/post/1332.html

展开阅读全文

4 评论

留下您的评论.