//创建一个容器来保存在listview里面positioni,
private HashMap<Integer, String> VIP1 = new HashMap<Integer, String>();
public class ListHolder {
private EditText Vip1;MyTextWatcher1 mTextWatcher1;
// 动态更新TextWathcer的position
public void updatePosition1(int position) {
mTextWatcher1.updatePosition(position);
}
//重写TextWatcher这个方法
class MyTextWatcher1 implements TextWatcher {
// 由于TextWatcher的afterTextChanged中拿不到对应的position值,所以自己创建一个子类
private int mPosition;
public void updatePosition(int position) {
mPosition = position;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
//保存对应position里面的内容
VIP1.put(mPosition, s.toString());}
}
/**
* EditText竖直方向是否可以滚动
*
* @param editText
* 需要判断的EditText
* @return true:可以滚动 false:不可以滚动
*/
private boolean canVerticalScroll(EditText editText) {
// 滚动的距离
int scrollY = editText.getScrollY();
// 控件内容的总高度
int scrollRange = editText.getLayout().getHeight();
// 控件实际显示的高度
int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop()
- editText.getCompoundPaddingBottom();
// 控件内容总高度与实际显示高度的差值
int scrollDifference = scrollRange - scrollExtent;
if (scrollDifference == 0) {
return false;
}
return (scrollY > 0) || (scrollY < scrollDifference - 1);
}
public View getView(final int position, View convertView, ViewGroup parent) {
Log.e("position.size()", position + "");
ListHolder list = null;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.standard_list_item, null);
list = new ListHolder();
list.Name = (TextView) convertView.findViewById(R.id.standard_list_tv);
list.Vip1 = (EditText) convertView.findViewById(R.id.standard_list_VIP1);
list.Vip1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 注意,此处必须使用getTag的方式,不能将position定义为final,写成mTouchItemPosition
// = position
mTouchItemPosition = (Integer) v.getTag();
// 触摸的是EditText并且当前EditText可以滚动则将事件交给EditText处理;否则将事件交由其父类处理
if ((v.getId() == R.id.standard_list_VIP1 && canVerticalScroll((EditText) v))) {
v.getParent().requestDisallowInterceptTouchEvent(true);
if (event.getAction() == MotionEvent.ACTION_UP) {
v.getParent().requestDisallowInterceptTouchEvent(false);
}
}
return false;
}
});
// 让ViewHolder持有一个TextWathcer,动态更新position来防治数据错乱;不能将position定义成final直接使用,必须动态更新
list.mTextWatcher1 = new MyTextWatcher1();
list.updatePosition1(position);
convertView.setTag(list);
} else {
list = (ListHolder) convertView.getTag();
list.updatePosition1(position);
}
if (VIP1.get(position)!=null) {
list.Vip1.setText(VIP1.get(position));
}else{
list.Vip1.setText("0.00");
}
list.Vip1.setTag(position);
if (mTouchItemPosition == position) {
list.Vip1.requestFocus();
list.Vip1.setSelection(list.Vip1.getText().length());
} else {
list.Vip1.clearFocus();
}
return convertView;
}
}