安卓解决listview的item嵌套多个edittext的问题

时间:2022-08-11 22:40:38

看过很多解决方式,但都会出现无法获取焦点的问题,但合适的只有一种,并且百试不爽,下面会详细介绍:

1、adapter里的关键代码

有两个edit,一个name一个price

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;

if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.chat_creat_order_item, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}

final ChatOrderBean bean = list.get(position);

//This is important. Remove TextWatcher first.
if (viewHolder.parts_name.getTag() instanceof TextWatcher) {
viewHolder.parts_name.removeTextChangedListener((TextWatcher) viewHolder.parts_name.getTag());
}
if (viewHolder.parts_price.getTag() instanceof TextWatcher) {
viewHolder.parts_price.removeTextChangedListener((TextWatcher) viewHolder.parts_price.getTag());
}


TextWatcher nameWatcher = 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 (TextUtils.isEmpty(s)) {
bean.setAccessoriesName("");
} else {
bean.setAccessoriesName(s.toString());
}
}
};
TextWatcher priceWatcher = 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 (TextUtils.isEmpty(s)) {
bean.setPrice(0.0);
} else {
bean.setPrice(Double.valueOf(s.toString()));
}
}
};

viewHolder.parts_name.addTextChangedListener(nameWatcher);
viewHolder.parts_price.addTextChangedListener(priceWatcher);
viewHolder.parts_name.setTag(nameWatcher);
viewHolder.parts_price.setTag(priceWatcher);

return convertView;
}


上面代码基本可以实现,但还会弹出键盘重新刷新getview的问题,下面解决:

2、在manifest里对应activity设置:

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"