Arrayadapter的Spinner行为“怪异”

时间:2013-05-14 09:41:54

标签: android android-spinner

我有一个旋转器。我已经覆盖了自定义下拉列表getview 我在android 2.2中工作

问题是当重新打开选择器时,所选值被替换为“default”(空字段)插入值:下面要解释的图像,以及它们下面的被覆盖代码:

Initial activity

现在按下微调器:

selection

现在按下robs,再按下旋转器

Missing robs

下拉覆盖方法:

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        View v;
        ViewHolder holder;
        if (convertView == null) {
            v = View.inflate(ctx, R.layout.fav_spinner_item, null);
            holder = new ViewHolder();
            holder.indentationField = (ImageView) v
                    .findViewById(R.id.indent);
            holder.nameField = (TextView) v.findViewById(R.id.name);
            holder.levelCount = mList.get(position).getTier() != -1 ? mList
                    .get(position).getTier() : 0;
            holder.nameField.setText(mList.get(position).getDisplayName());
            if (holder.levelCount == 0) {
                holder.indentationField.setVisibility(View.GONE);
            } else {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.indentationField.getLayoutParams();
                params.setMargins(params.leftMargin+((params.leftMargin+15)*holder.levelCount), params.topMargin, params.rightMargin, params.bottomMargin);
                holder.indentationField.setLayoutParams(params);
            }
            v.setTag(holder);
        } else {
            v = convertView;
            holder = (ViewHolder) convertView.getTag();
        }

        return v;
    }

1 个答案:

答案 0 :(得分:1)

您没有正确使用ViewHolder模式。由于可以为任何项目索引调用getDropDownView(),并且可以传递任何回收View,因此您需要正确重置所有动态数据每次。像这样:

@Override
public View getDropDownView(int position, View convertView,
        ViewGroup parent) {
    View v;
    ViewHolder holder;
    if (convertView == null) {
        v = View.inflate(ctx, R.layout.fav_spinner_item, null);
        holder = new ViewHolder();
        holder.indentationField = (ImageView) v
                .findViewById(R.id.indent);
        holder.nameField = (TextView) v.findViewById(R.id.name);
        v.setTag(holder);
    } else {
        v = convertView;
        holder = (ViewHolder) convertView.getTag();
    }
    // The rest of this is dynamic data, based on the item position
    holder.levelCount = mList.get(position).getTier() != -1 ? mList
                .get(position).getTier() : 0;
    holder.nameField.setText(mList.get(position).getDisplayName());
    if (holder.levelCount == 0) {
        holder.indentationField.setVisibility(View.GONE);
    } else {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.indentationField.getLayoutParams();
        params.setMargins(params.leftMargin+((params.leftMargin+15)*holder.levelCount), params.topMargin, params.rightMargin, params.bottomMargin);
        holder.indentationField.setLayoutParams(params);
        holder.indentationField.setVisibility(View.VISIBLE);
    }
    return v;
}

另请注意我添加的行,以重置indentationFieldVISIBLE的可见性,因为您可能正在回收View,其中可见性先前已设置为GONE