Android|Android recyclerview嵌套多个EditText 请EditText输入上下联动示例
【Android|Android recyclerview嵌套多个EditText 请EditText输入上下联动示例】**
需求 recyclerview的item里包含两个EditText 并且 两个控件值得改变可以互相影响(示例里只包含两EditText相关代码) **
文章图片
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import org.elastos.wallet.R;
import org.elastos.wallet.ela.base.BaseFragment;
import org.elastos.wallet.ela.ui.vote.bean.VoteListBean;
import org.elastos.wallet.ela.utils.AppUtlis;
import org.elastos.wallet.ela.utils.Log;
import org.elastos.wallet.ela.utils.NumberiUtil;
import java.util.HashMap;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class NodeCartAdapter extends RecyclerView.Adapter {
private List list;
private Context context;
private double balance;
public void setBalance(double balance) {
this.balance = balance;
}public NodeCartAdapter(List list, BaseFragment context) {
this.context = context.getContext();
this.list = list;
}@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_cr_nodcart, viewGroup, false);
return new MyViewHolder(view);
}@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {holder.etTicketnum.setTag(null);
//第一个输入框
holder.etTicketpercent.setTag(null);
;
//第二个输入框if (holder.etTicketnum.getTag(R.string.ownerpublicKey) != null) {
TextWatcher textWatcher = (TextWatcher) holder.etTicketnum.getTag(R.string.ownerpublicKey);
holder.etTicketnum.removeTextChangedListener(textWatcher);
}
//因为listener是个数组也就是可以多个同时监听为了避免多余的监听,先把以前的移除
if (holder.etTicketpercent.getTag(R.string.ownerpublicKey) != null) {
TextWatcher textWatcher = (TextWatcher) holder.etTicketpercent.getTag(R.string.ownerpublicKey);
holder.etTicketpercent.removeTextChangedListener(textWatcher);
}
holder.etTicketnum.setText(null);
holder.etTicketpercent.setText(null);
//此处已经没有任何监听 如果为editText写入默认值可以写在这里
addTextChangedListener(holder.etTicketnum, holder.etTicketpercent, position);
addTextChangedListener(holder.etTicketpercent, holder.etTicketnum, position);
}@Override
public int getItemCount() {
return list.size();
}class MyViewHolder extends RecyclerView.ViewHolder {@BindView(R.id.et_ticketnum)
EditText etTicketnum;
@BindView(R.id.et_ticketpercent)
EditText etTicketpercent;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}private void addTextChangedListener(EditText editText, EditText editText2, int position) {
if (editText.getTag() != null) {
editText.setTag(false);
return;
}
TextWatcher textWatcher = 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) {
//拦截因为自己在此方法内修改内容导致的多次触发TextChangedListener(editText2.setTag(true)标识是方法内改变的不需要再次处理)
if (editText.getTag() != null && (boolean) editText.getTag()) {
editText.setTag(false);
//重置防止多次触发的条件
return;
}if (TextUtils.isEmpty(s)) {
editText2.setTag(true);
//防止多次触发TextChangedListener
editText2.setText("");
dataMapELA.put(position, 0.0);
return;
}
String input = s.toString().trim();
try {
double inputValue = https://www.it610.com/article/Double.parseDouble(input);
switch (editText.getId()) {
case R.id.et_ticketnum:
dataMapELA.put(position, inputValue);
editText2.setTag(true);
editText2.setText(NumberiUtil.numberFormat(inputValue / balance * 100, 2));
break;
case R.id.et_ticketpercent:
dataMapELA.put(position, inputValue / 100 * balance);
editText2.setTag(true);
editText2.setText(NumberiUtil.numberFormat(inputValue / 100 * balance, 8));
break;
}
} catch (Exception e) {
dataMapELA.put(position, 0.0);
editText.setTag(true);
editText2.setTag(true);
editText2.setText("");
editText.setText("");
}}
};
editText.addTextChangedListener(textWatcher);
editText.setTag(R.string.ownerpublicKey,textWatcher);
}}
推荐阅读
- android第三方框架(五)ButterKnife
- mybatisplus|mybatisplus where QueryWrapper加括号嵌套查询方式
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- android防止连续点击的简单实现(kotlin)