我是靠谱客的博主 丰富音响,这篇文章主要介绍RecyclerView中item的EditText的监听,现在分享给大家,希望可以做个参考。

1.item布局

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

这里写图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.效果图
这里写图片描述

最后

以上就是丰富音响最近收集整理的关于RecyclerView中item的EditText的监听的全部内容,更多相关RecyclerView中item内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部