?核心代码1
viewHolder.etPlanCount.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) {
// 根据EditText的标签,更新数据源中的数据
int pos = (int) viewHolder.etPlanCount.getTag();
int count = s.toString().equals("") ? 0 : Integer.parseInt(s.toString());
mPlanScanProductInfoList.get(pos).setPlanCount(count);
onInputTextListener.afterChangedText(mPlanScanProductInfoList.get(pos).getProductNo(), count + "");
}
});
注意事项: 编辑框设置tag要在holder为null的?判断里添加(好像不是,再试试)
?参考:?
参考 :android ListView与EditText共存错位 - 冷冷汤圆 - 博客园
public class OutNoBillProductAdapter extends BaseAdapter {
/* 输入文本以后获取文本的监听事件 */
private OnInputTextListener onInputTextListener;
public interface OnInputTextListener {
void afterChangedText(String productNo, String text);
}
/* 适配器 */
private Context mContext;
private List<PlanScanProductInfo> mPlanScanProductInfoList;
public OutNoBillProductAdapter(Context context, List<PlanScanProductInfo> planScanProductInfoList, OnInputTextListener onInputTextListener) {
this.mContext = context;
this.mPlanScanProductInfoList = planScanProductInfoList;
this.onInputTextListener = onInputTextListener;
}
@Override
public int getCount() {
return mPlanScanProductInfoList != null ? mPlanScanProductInfoList.size() : 0;
}
@Override
public Object getItem(int position) {
return mPlanScanProductInfoList != null ? mPlanScanProductInfoList.get(position) : null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_out_not_bill_item, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.tvProductNo.setText(mPlanScanProductInfoList.get(position).getProductNo());
viewHolder.tvProductName.setText(mPlanScanProductInfoList.get(position).getProductName());
// 在滑动的过程中,给每一个EditText设置其唯一的标签,动态的将item与position进行绑定,以便于监听的EditText文本内容改变,当内容改变时可以根据该标签去更新数据源中相应位置中数据。
viewHolder.etPlanCount.setTag(position);
viewHolder.etPlanCount.clearFocus();
viewHolder.etPlanCount.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) {
// 根据EditText的标签,更新数据源中的数据
int pos = (int) viewHolder.etPlanCount.getTag();
int count = s.toString().equals("") ? 0 : Integer.parseInt(s.toString());
mPlanScanProductInfoList.get(pos).setPlanCount(count);
onInputTextListener.afterChangedText(mPlanScanProductInfoList.get(pos).getProductNo(), count + "");
}
});
viewHolder.etPlanCount.setText(mPlanScanProductInfoList.get(position).getPlanCount() + "");
// 光标定位到内容之后
viewHolder.etPlanCount.setSelection(String.valueOf(mPlanScanProductInfoList.get(position).getPlanCount()).length());
return convertView;
}
final class ViewHolder {
public TextView tvProductNo, tvProductName;
public EditText etPlanCount;
public ViewHolder(View view) {
tvProductNo = view.findViewById(R.id.tv_product_no);
tvProductName = view.findViewById(R.id.tv_product_name);
etPlanCount = view.findViewById(R.id.et_plan_count);
}
}
private abstract class MyTextWatcher implements TextWatcher{
private ViewHolder mHolder;
public MyTextWatcher(ViewHolder holder) {
this.mHolder=holder;
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable s) {
afterTextChanged(s, mHolder);
}
public abstract void afterTextChanged(Editable s,ViewHolder holder);
}
}
实际操作:
ViewHolder holder = null;
if (convertView == null) {
convertView = UIUtils.inflate(R.layout.adp_item_tank);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.adpItemGasolineNum.setTag(position);
holder.adpItemNameTank.setText((position + 1) + "号油罐");
holder.adpItemRg.check(StringUtils.equals(oiltanks.get(position).getOli_type(), "柴油") ? R.id.adp_item_rb_diesel : R.id.adp_item_rb_gasoline);
oiltanks.get(position).setOli_type(holder.mainRbDiesel.isChecked() ? "柴油" : "汽油");
holder.adpItemGasolineNum.setText(StringUtil.valueOf(oiltanks.get(position).getGuanCapacity()));
holder.adpItemGasolineNum.addTextChangedListener(new MyTextWatcher(holder) {
@Override
public void afterTextChanged(Editable s, ViewHolder holder) {
int position=(Integer) holder.adpItemGasolineNum.getTag();
oiltanks.get(position).setGuanCapacity(s.toString().trim());
}
});
return convertView;
|