概述
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.效果图
最后
以上就是丰富音响为你收集整理的RecyclerView中item的EditText的监听的全部内容,希望文章能够帮你解决RecyclerView中item的EditText的监听所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复