0

我有一个微调器。我已经覆盖了自定义下拉 getview。
我正在使用 android 2.2

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

初始活动

现在按下微调器:

选择

现在按下 rob,然后再次按下微调器

失踪的抢劫

下拉覆盖方法:

    @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;
    }
4

1 回答 1

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先前设置为可见性的aGONE

于 2013-05-14T10:20:46.360 回答