RecyclerView中item的EditText的监听

1.item布局

  • 两个文本框,输入内容,外部监听内容改变,获取输入内容,保存起来;
  • 出现问题:添加item,holder的复用会导致里面的数据会出现错乱;

这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="5dp"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="满"
        android:textColor="@color/heishe"
        />
    <EditText
        android:id="@+id/et_price"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:hint="价格"
        android:singleLine="true"
        android:ellipsize="end"
        android:paddingLeft="3dp"
        android:textSize="@dimen/font_size_small"
        android:textColorHint="@color/text_gray"
        android:background="@drawable/shape_white_border"
        android:layout_marginLeft="3dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="元    减"
        android:textColor="@color/heishe"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        />
    <EditText
        android:id="@+id/et_sub"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:hint="减多少"
        android:singleLine="true"
        android:ellipsize="end"
        android:paddingLeft="3dp"
        android:textSize="@dimen/font_size_small"
        android:background="@drawable/shape_white_border"
        android:layout_marginLeft="3dp"
        />
</LinearLayout>

2.Adapter

public class SubValuesRecycleAdapter extends RecyclerView.Adapter<SubValuesRecycleAdapter.MyViewHolder> {

    private LinkedList<PriceSub> priceSubPairs;
    private Context context;

    public SubValuesRecycleAdapter(Context context, LinkedList<PriceSub> priceSubPairs) {
        this.priceSubPairs = priceSubPairs;
        this.context = context;
    }

    @Override
    public SubValuesRecycleAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycle_sub, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SubValuesRecycleAdapter.MyViewHolder holder, final int position) {
        holder.etPrice.setTag(position);
        holder.etSub.setTag(position);
        holder.etPrice.setText(priceSubPairs.get(position).getPrice());
        holder.etSub.setText(priceSubPairs.get(position).getSub());
        holder.etPrice.addTextChangedListener(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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (holder.etPrice.getTag() == position) {//设置tag解决错乱问题
                    onPriceFillListener.onPriceFill(position, s.toString());
                }
            }
        });
        holder.etSub.addTextChangedListener(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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (holder.etSub.getTag() == position) {
                    onSubFillListener.onSubFill(position, s.toString());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return priceSubPairs == null ? 0 : priceSubPairs.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        EditText etPrice, etSub;

        public MyViewHolder(View itemView) {
            super(itemView);
            etPrice = (EditText) itemView.findViewById(R.id.et_price);
            etSub = (EditText) itemView.findViewById(R.id.et_sub);
        }
    }

    private OnPriceFillListener onPriceFillListener;
    private OnSubFillListener onSubFillListener;

    public interface OnPriceFillListener {
        void onPriceFill(int position, String price);
    }

    public interface OnSubFillListener {
        void onSubFill(int position, String sub);
    }

    public void setOnPriceFillListener(OnPriceFillListener onPriceFillListener) {
        this.onPriceFillListener = onPriceFillListener;
    }

    public void setOnSubFillListener(OnSubFillListener onSubFillListener) {
        this.onSubFillListener = onSubFillListener;
    }
}

3.Activity中设置adapter

        private SubValuesRecycleAdapter adapter;
        private LinkedList<PriceSub> priceSubList;
        private RecyclerView rvSubValues;
        private LinearLayoutManager layoutManager;

        rvSubValues = (RecyclerView) view.findViewById(R.id.rv_sub_value);
        layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        layoutManager.setStackFromEnd(true);
        rvSubValues.setLayoutManager(layoutManager);
        priceSubList = new LinkedList<>();
        priceSubList.add(new PriceSub());
        priceSubList.add(new PriceSub());
        priceSubList.add(new PriceSub());
        adapter = new SubValuesRecycleAdapter(getActivity(), priceSubList);
        adapter.setOnPriceFillListener(this);
        adapter.setOnSubFillListener(this);
        rvSubValues.setAdapter(adapter);

     @Override
        public void onPriceFill(int position, String price) {
            //输入的价格监听
            //判断当前位置是否存在,因为删除item会触发文本改变事件afterTextChanged(Editable s)
            if (position < priceSubList.size()) {
                priceSubList.get(position).setPrice(price);
                Log.d("Price", "onPriceFill: " + priceSubList);
            }
        }

        @Override
        public void onSubFill(int position, String sub) {
            //减多少监听
            //判断当前位置是否存在,因为删除item会触发文本改变事件afterTextChanged(Editable s)
            if (position < priceSubList.size()) {
                priceSubList.get(position).setSub(sub);
                Log.d("Price", "onSubFill: " + priceSubList);
            }
        }
        @Override
            public void onClick(View v) {
                int id = v.getId();
                switch (id) {
                    case R.id.tv_add:
                        if (priceSubList != null) {
                            priceSubList.add(new PriceSub());
                            adapter.notifyDataSetChanged();
                        }
                        break;
                    case R.id.tv_delete:
                        if (priceSubList != null && priceSubList.size() > 0) {
                            priceSubList.removeLast();
                            adapter.notifyDataSetChanged();
                        }
                        break;
                }
            }

4.效果图
这里写图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值