9

我将 ArrayAdapter 用于我自己的对象类型(只有一种类型)的列表,并且我为用户提供了创建更多项目的选项(从而为这些项目创建更多视图)。在某些时候,getView 发送了一个带有非空“convertView”的新“位置”索引。然后它在最后一个位置显示第一个视图。在那之后,当滚动视图时,所有的视图都混在一起了。我假设这意味着我以不应该的方式操纵了视图,但我只是看不到在哪里。这是一些代码:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    PreviewItemHolder holder = null;

    // Initialize view if convertview is null
    if (convertView == null) {
        v = newView(parent, position);
    }
    // Populate from previously saved holder
    else {
        // Use previous item if not null
        v = convertView;
    }

    // Populate if the holder is null (newly inflated view) OR
    // if current view's holder's flag is true and requires populating
    if ((holder == null) || (holder.readPopulateFlag())) {
        bindView(position, v);
    }

    return v;
}

    private View newView(ViewGroup parent, int position) {
    // Getting view somehow...
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View inflatedView = inflater.inflate(R.layout.preview_element_set, parent, false);
    PreviewItemHolder holder = new PreviewItemHolder();

    holder.set = (Set) mSets.get(position);
    holder.previewElementHolders = new ArrayList<PreviewElementHolder>();
    holder.expandArea = (View) inflatedView.findViewById(R.id.expandArea);
    holder.repetitionsLabel = (TextView) inflatedView.findViewById(R.id.previewRepetitionsInput);
    holder.endlessInput = (CheckBox) inflatedView.findViewById(R.id.previewSetEndlessInput);
    holder.nameLabel = (TextView) inflatedView.findViewById(R.id.previewSetNameLabel);
    holder.commentInput = (EditText) inflatedView.findViewById(R.id.previewSetCommentInput);
    holder.soundInput = (EditText) inflatedView.findViewById(R.id.previewSetSoundInput);
    holder.addElementButton = (Button) inflatedView.findViewById(R.id.previewSetAddElements);
    holder.expand = (View) inflatedView.findViewById(R.id.infoArea);
    holder.collapse = (View) inflatedView.findViewById(R.id.collapse);

    final int setsLength = holder.set.getElements().size();

    for (int i = 0; i < setsLength; i++) {
        AElement currElement = holder.set.getElements().get(i);

        // Creating new element holder according to the type
        if (currElement instanceof Rest) {
            holder.previewElementHolders.add(new PreviewRestHolder());
        }
        else if (currElement instanceof TimeExercise) {
            holder.previewElementHolders.add(new PreviewTimeExerciseHolder());
        }
        else if (currElement instanceof RepetitionExercise) {
            holder.previewElementHolders.add(new PreviewRepetitionExerciseHolder());
        }

        View currLayout = inflateElement(currElement, inflater, i, holder.previewElementHolders.get(i));

        // Add the child before the hairline, collapse image and the add
        // button
        // (3 last children of the expandArea view
        ((ViewGroup) holder.expandArea).addView(currLayout, ((ViewGroup) holder.expandArea).getChildCount() - CHILDREN_INDEX_AFTER_PHASES_LABEL);
    }

    inflatedView.setTag(holder);

    return inflatedView;
}

private void bindView(int position, View inflatedView) {
    final PreviewItemHolder holder = (PreviewItemHolder) inflatedView.getTag();
    holder.set.setId(position);
    holder.endlessInput.setChecked(holder.set.getEndless());
    holder.soundInput.setText(holder.set.getSound());
    holder.nameLabel.setText(holder.set.getName());
    holder.commentInput.setText(holder.set.getComment());

    // Make sure there is a name. If none, put default
    if (holder.nameLabel.getText().equals("")) {
        holder.nameLabel.setText(R.string.default_set_name);
    }

    // Set repetitions value according to the endless flag
    if (holder.set.getEndless()) {
        holder.repetitionsLabel.setText(R.string.infinity);
    }
    else {
        holder.repetitionsLabel.setText(String.valueOf(holder.set.getRepetitions()));
    }

    // Set click listeners
    holder.endlessInput.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            // Save endless flag
            holder.set.setEndless(isChecked);

            // If an endless set - Dropset
            if (isChecked) {
                holder.repetitionsLabel.setText(R.string.infinity);
            }
            else {
                // Regular set
                holder.repetitionsLabel.setText(String.valueOf(holder.set.getRepetitions()));
            }

            hideShowRepsWeights(holder);
        }

    });

    holder.repetitionsLabel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            NumericDialog instance = NumericDialog.newInstance(holder, holder.set, NumericDialog.INTEGER_MODE, Consts.SET_REPETITIONS_METHOD_NAME);
            instance.show(((Activity) getContext()).getFragmentManager(), null);
        }
    });

    holder.nameLabel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Setting flag to true to allow populating this view
            holder.rePopulateFlag = true;
            SetNameDialog instance = SetNameDialog.newInstance(holder.set);
            instance.show(((Activity) getContext()).getFragmentManager(), null);
        }
    });

    holder.commentInput.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                // After focus is lost, save the text into the set
                holder.set.setComment(holder.commentInput.getText().toString());
            }
        }
    });

    // TODO Change that into a dialog that allows selection of sounds
    holder.soundInput.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                // After focus is lost, save the text into the set
                holder.set.setSound(holder.soundInput.getText().toString());
            }
        }
    });

    holder.expand.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Change visibility - Show expandArea and its data
            holder.expandArea.setVisibility(View.VISIBLE);
            holder.expand.setVisibility(View.GONE);
            holder.collapse.setVisibility(View.VISIBLE);
        }
    });

    holder.collapse.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Change visibility - Hide expandArea and its data
            holder.expandArea.setVisibility(View.GONE);
            holder.collapse.setVisibility(View.GONE);
            holder.expand.setVisibility(View.VISIBLE);
        }
    });

    holder.addElementButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            AddElementDialog instance = AddElementDialog.newInstance(holder);
            instance.show(((Activity) getContext()).getFragmentManager(), null);
        }
    });

    // Populate elements
    for (PreviewElementHolder elementHolder : holder.previewElementHolders) {
        populateElement(elementHolder, holder);
    }

    // Finally hide/show if needed - Should this be put somewere else?
    hideShowRepsWeights(holder);
}

请告诉我您是否认为我应该上传更多方法以使事情更清楚。

4

3 回答 3

15

一位朋友向我解释了这个问题,现在它似乎工作了。基本上 ListView 只持有少量的视图,并且一直在回收它们。在我的情况下,我有一个 Nexus 4,所以它似乎总共有 7 个视图,因为第 8 个总是开始引起麻烦的那个。我在 getView() 中缺少的条件是检查位置与 ArrayAdapter 中当前项目的 ID 之间的相关性。以下是它现在的样子:

@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
    View v;
    PreviewItemHolder holder = null;

    // Initialize view if convertview is null
    if (convertView == null) {
        v = newView(parent, position);
    }
    // Populate from previously saved holder
    else {
        // If position and id of set do not match, this view needs to be re-created, not recycled
        if (((PreviewItemHolder) convertView.getTag()).set.getId() != position) {
            v = newView(parent, position);
        }
        else {
            // Use previous item if not null
            v = convertView;

            // Get holder
            holder = (PreviewItemHolder) v.getTag();
        }
    }

    // Populate if the holder is null (newly inflated view) OR
    // if current view's holder's flag is true and requires populating
     if (holder == null || holder.readPopulateFlag()) {
         bindView(position, v);
     }

    return v;
}
于 2013-08-23T08:49:14.367 回答
1

你需要bindView() 总是打电话。想法或重用如下。如果convertView为 null,则创建并初始化一个新视图。如果convertView不为空,则采用此视图并将其转换为新视图,这意味着您bindView()使用convertView实例调用。

查看此 Javadoc了解更多详细信息。

于 2013-08-19T22:16:14.200 回答
0

当您有几种类型的视图应该被回收时,您必须告诉列表视图您通过实现该方法获得了多少种类型

@Override
    public int getViewTypeCount() {
        return 7;
    }
于 2015-08-18T13:11:29.553 回答