一般都会出现的问题:
1.在listview中editetext焦点的问题 .
2.Listview复用机制导致editext内容错乱问题.
一、在listview中editetext焦点的问题?
需要在xml布局中给listview设置一个descendantFocusability属性,这个 属性一共有三个值beforeDescendants、afterDescendants、blocksDescendants。
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
在这里我给listview设置descendantFocusability属性为beforeDescendants,当然也可以在itemview布局的xml中为其editetext设置descendantFocusability属性为blocksDescendants,两种方式效果都一样。
<ListView
android:id="@+id/lv_person_info"
android:descendantFocusability="beforeDescendants"
android:divider="@null"
android:cacheColorHint="@color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
二、Listview复用机制导致editext内容错乱问题?
复用问题 处理也比较简单,就是给每个item设置游标,当内容改变时改变对应的数据源,为空时就清除editText的内容,直接看代码好了;
public class MatchEnrollPersonCustomInfoAdapter extends BaseAdapter{
private Context context;
private List<MatchEnrollPersonDefine> datas;
private boolean onlylook = false;
public MatchEnrollPersonCustomInfoAdapter(Context context, List<MatchEnrollPersonDefine> datas,boolean onlylook) {
this.context = context;
this.datas = datas;
this.onlylook = onlylook;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHodler hodler;
if(convertView == null){
hodler = new ViewHodler();
convertView = LayoutInflater.from(context).inflate(R.layout.adapter_person_custom_info_item,parent,false);
hodler.tvTitle = (TextView) convertView.findViewById(R.id.tv_person_info_title);
hodler.edContent = (EditText) convertView.findViewById(R.id.ed_person_info_content);
hodler.edContent.addTextChangedListener(new MyTextWatcher(hodler.edContent));
convertView.setTag(hodler);
}else {
hodler = (ViewHodler) convertView.getTag();
}
// 业务需求,是否可操作 输入框
if(onlylook){
hodler.edContent.setEnabled(false);
}else {
hodler.edContent.setEnabled(true);
}
hodler.edContent.setTag(position);
// TODO: 2018/11/9 设置数据源
hodler.tvTitle.setText(datas.get(position).remark);
hodler.edContent.setHint(datas.get(position).input_value+"");
if (!TextUtils.isEmpty(datas.get(position).value)) {//不为空的时候 赋值给对应的edittext
hodler.edContent.setText(datas.get(position).value);
hodler.edContent.setSelection(datas.get(position).value.length());
} else {//置空
hodler.edContent.getEditableText().clear();
}
return convertView;
}
class ViewHodler{
TextView tvTitle;
EditText edContent;
}
class MyTextWatcher implements TextWatcher{
EditText editText;
public MyTextWatcher(EditText editText){
this.editText = editText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int mTouchItemPosition = (int) editText.getTag();
MatchEnrollPersonDefine bean = datas.get(mTouchItemPosition);
bean.value = s.toString();
}
@Override
public void afterTextChanged(Editable s) {
}
}
}
以上感觉是解决此类问题比较简便的方式了,还有的大佬通过设置OnTouch事件,手动设置焦点,感觉还是比较繁琐的。