public class SetAdapter extends ArrayAdapter {
private List<SetData> _content; private final int _resourceId; private int _positionOnTouch = -1; private OnChangedTextListener _editChangeListener;
interface OnChangedTextListener { void onChangedTextListener(int position, String str); }
class ViewHolder implements TextWatcher { int position; TextView tvName; TextView tvRange; TextView tvValue; EditText edtText; boolean notifyEnable = false;
ViewHolder(View view, int position, int... id) { this.position = position; this.tvName = (TextView) view.findViewById(id[0]); this.tvRange = (TextView) view.findViewById(id[1]); this.tvValue = (TextView) view.findViewById(id[2]); this.edtText = (EditText) view.findViewById(id[3]); }
public void updatePosition(int position) { this.position = position; }
@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 (notifyEnable && _editChangeListener != null) { _editChangeListener.onChangedTextListener(this.position, s.toString()); } } }
public SetAdapter(Context context, int textViewResourceId, List<SetData> objects) { super(context, textViewResourceId, objects); _resourceId = textViewResourceId; _content = objects; _editChangeListener = new OnChangedTextListener() { @Override public void onChangedTextListener(int position, String str) { SetData item = _content.get(position); item.setFiled(3, str); } }; }
public List<SetData> getContent() { return _content; }
@Override public View getView(int position, View convertView, ViewGroup viewGroup) {
//String info = String.format("into getView, pos=%d", position); //Log.e("gmx", info);
ViewHolder vh; if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(_resourceId, null); vh = new ViewHolder(convertView, position, R.id.list_name, R.id.list_range, R.id.list_value, R.id.list_value_set); convertView.setTag(vh); } else { vh = (ViewHolder) convertView.getTag(); vh.updatePosition(position); }
SetData item = (SetData) getItem(position);
vh.notifyEnable = false;
vh.edtText.setTag(vh);
vh.edtText.setText(item.getField(3));
vh.tvName.setText(item.getField(0)); // vh.tvName.setTextColor(item.textColor[0]); // vh.tvName.setBackgroundColor(item.bkColor[0]);
vh.tvRange.setText(item.getField(1)); // vh.tvRange.setTextColor(item.textColor[1]); // vh.tvRange.setBackgroundColor(item.bkColor[1]); vh.tvValue.setText(item.getField(2)); // vh.tvValue.setTextColor(item.textColor[2]); // vh.tvValue.setBackgroundColor(item.bkColor[2]);
vh.edtText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((ViewGroup) v.getParent()).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); ViewHolder viewHolder = (ViewHolder) v.getTag(); _positionOnTouch = viewHolder.position; ((EditText) v).addTextChangedListener(viewHolder); return false; } }); convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((ViewGroup) v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); return false; } }); vh.notifyEnable = true; if (_positionOnTouch == position) { vh.edtText.requestFocus(); vh.edtText.setSelection(vh.edtText.getText().length()); } else { vh.edtText.clearFocus(); } return convertView; } }
|