从RecyclerView中删除项目时,如何重设文本彻底

时间:2018-12-25 17:15:12

标签: android android-recyclerview

即使从列表中删除了项目,stikethough和文本颜色也不会更新(重置)。因此,当我添加新项目时,文本和文本颜色会出现意外的变形。我试图通过在onBindHolder中添加“ paint.setFlag(holder.itemName.getPaintFlags()^ Paint.STRIKE_THRU_TEXT_FLAG)来解决此问题,但这没有用...

如何解决此问题? https://github.com/kitomiyu/practice-hospitalList/issues/5

    @Override
public void onBindViewHolder(@NonNull TaskItemViewHolder holder, int position) {
    if (mItems != null) {
        TodoItem current = mItems.get(position);
        holder.itemName.setText(current.getText());
        holder.itemCheckBox.setChecked(false);
    }
}

// provide a reference to the views for each data
class TaskItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private final TextView itemName;
    private final CheckBox itemCheckBox;

    TaskItemViewHolder(View itemView) {
        super(itemView);
        itemName = itemView.findViewById(R.id.taskName);
        itemCheckBox = itemView.findViewById(R.id.taskCheck);

        itemCheckBox.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int adapterPosition = getAdapterPosition();
        TodoItem current = mItems.get(adapterPosition);
        TextPaint paint = itemName.getPaint();

        if (itemCheckBox.isChecked()) {
            // change color
            itemName.setTextColor(Color.LTGRAY);
            // add strikethrough
            paint.setFlags(itemName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            paint.setAntiAlias(true);

            // when item is checked, add the item as delete target
            mDeleteItems.add(current);
            mClickListener.onItemClick(mDeleteItems);
        } else {
            mDeleteItems.remove(current);
            itemName.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
            paint.setFlags(itemName.getPaintFlags() ^ Paint.STRIKE_THRU_TEXT_FLAG);
            paint.setAntiAlias(false);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您在if else的两种情况下都设置了击穿。