1.焦点问题:
xml布局中给listview设置一个descendantFocusability属性
android:descendantFocusability="beforeDescendants"
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
2. 数据混乱问题:
利用一个map去保存每个edittext的内容,然后在赋值的时候在从map中取出来,如果有就赋值,否则置空
事例代码:
public class FillInfoAdapter extends BaseAdapter {
public HashMap<Integer, String> saveMap = new HashMap<>();
private Context context;
private ArrayList<FillBean> data;
public FillInfoAdapter(Context context, ArrayList<FillBean> data) {
this.context = context;
this.data = data;
}
public void setData(ArrayList<FillBean> data) {
this.data = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_fillinfo, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.setData(position);
return convertView;
}
private class ViewHolder {
TextView tvTitle;
EditText etValue;
ViewHolder(View view) {
tvTitle = view.findViewById(R.id.tvTitle);
etValue = view.findViewById(R.id.etValue);
etValue.addTextChangedListener(new MyTextChangedListener(this, saveMap));
}
public void setData(int position) {
tvTitle.setText(data.get(position).getName());
etValue.setTag(position);//注意设置position
if (!TextUtils.isEmpty(saveMap.get(position))) {//不为空的时候 赋值给对应的edittext
etValue.setText(saveMap.get(position));
} else {//置空
etValue.getEditableText().clear();
}
}
}
public class MyTextChangedListener implements TextWatcher {
public ViewHolder holder;
public HashMap<Integer, String> contents;
public MyTextChangedListener(ViewHolder holder, HashMap<Integer, String> contents) {
this.holder = holder;
this.contents = contents;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (holder != null && contents != null) {
int position = (int) holder.etValue.getTag();
contents.put(position, editable.toString());
//将edittext中的值赋值进data就可以在activity中data.get(i).getValue()获取输入框中的值
data.get(position).setValue(editable.toString());
}
}
}
}