可扩展的列表视图 - 禁用视图的回收

时间:2014-06-03 07:08:53

标签: android expandablelistview recycle

我在Android中使用Expandable listview,并且由于Android的回收政策,子项目中的交互式EditTexts存在巨大问题。

我想知道我是否能够以某种方式禁用这种回收(那里的元素不会太多,所以性能应该不是问题)?

问题是EditTexts中有重复项,即如果我在第一个EditText中键入某些内容,则可能在某些其他EditText中出现相同的文本,或者如果我折叠并展开一个组,则字符串在EditTexts经常混淆。

关注我的代码:

修改

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildItem item = getChild(groupPosition, childPosition);
    String comment = "";
    state = getStatelist().get(groupPosition);

    if (convertView == null) {
        holder = new ChildHolder();           
        convertView = inflater.inflate(R.layout.childitem_list_item, parent, false);
        holder.title = (EditText) convertView.findViewById(R.id.textTitle);          
        convertView.setTag(holder);
    } else {
        holder = (ChildHolder) convertView.getTag();
    }

    if(state.commentList != null){
        holder.title.setText(state.commentList[childPosition]);
    }
    else{
        holder.title.setText("");
    }

    holder.title.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            state = getStatelist().get(groupPosition);

            state.commentList[childPosition] = s.toString();                

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}


public class States {

  public String code = "";
  public String name = "";
  public String[] commentList;
 }

1 个答案:

答案 0 :(得分:1)

你有两个不同的问题。你想要的是清理每次重建中edittexts的值。所以你可以改变

  if(state.commentList != null){
        holder.title.setText(state.commentList[childPosition]);
    }
    else{
        holder.title.setText("");
    }

holder.title.setText("");
  if(state.commentList != null){
        holder.title.setText(state.commentList[childPosition]);
    }

现在,如果您真正想要的是删除视图回收,只需使convertview始终重新填充

state = getStatelist().get(groupPosition);
holder = new ChildHolder();           
convertView = inflater.inflate(R.layout.childitem_list_item, parent, false);
holder.title = (EditText) convertView.findViewById(R.id.textTitle);          
convertView.setTag(holder);
holder = (ChildHolder) convertView.getTag();
// You can remove the viewholder pattern because you're not using it anymore
if(state.commentList != null){