在recyclerview android上单个选择的项目

时间:2016-04-19 15:50:38

标签: android android-recyclerview

用户点击时,我正在尝试在我的recyclerview上突出显示一项。但我有一个意想不到的行为。

当我点击一个项目时,适配器会记录我已选择并突出显示该项目。但是当我滚动时,我看到还有其他项目突出显示。

对此问题的任何帮助都将非常感激。

我的代码:

我的适配器

public class ChooseRecipientAdapter extends RecyclerView.Adapter<ChooseRecipientViewHolder> {


 @Override
public ChooseRecipientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_recipients_item, null);
    ChooseRecipientViewHolder cv = new ChooseRecipientViewHolder(this.context, v);
    return cv;
}

 @Override
public void onBindViewHolder(final ChooseRecipientViewHolder holder, final int position) {
    RecipientItem settingsRecipients = recipientItems.get(position);
    settingsRecipients.holder = holder;

    holder.title.setText(settingsRecipients.title);

    holder.recipient = recipients.get(position);

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

            if (selectedItem < 0) {
                selectedItem = position;
                holder.isSelected = true;
                holder.itemView.setSelected(true);
                holder.title.setTextColor(context.getResources().getColor(R.color.color_background));
            } else if (holder.isSelected) {
                selectedItem = -1;
                holder.isSelected = false;
                holder.itemView.setSelected(false);
                holder.title.setTextColor(context.getResources().getColor(R.color.textColorPrimary));
                ChooseRecipientActivity.recipient = null;

            } else {
                if (getItem(selectedItem).holder != null) {
                    getItem(selectedItem).holder.isSelected = false;
                    getItem(selectedItem).holder.itemView.setSelected(false);
                    getItem(selectedItem).holder.title.setTextColor(context.getResources().getColor(R.color.textColorPrimary));
                    selectedItem = position;
                    holder.isSelected = true;
                    holder.itemView.setSelected(true);
                    holder.title.setTextColor(context.getResources().getColor(R.color.color_background));
                    ChooseRecipientActivity.recipient = holder.recipient;

                } 
            }
        }
    });


}

我的ViewHolder

public class ChooseRecipientViewHolder extends RecyclerView.ViewHolder {


public String id;
public Context context;
public ImageView leftImageView;
public TextView title;
public boolean isSelected = false;
public Recipient recipient;
public View itemView;


public ChooseRecipientViewHolder(final Context context, View view) {
    super(view);
    this.context = context;
    this.leftImageView = (ImageView) view.findViewById(R.id.leftIcon);
    this.title = (TextView) view.findViewById(R.id.title);
    itemView = view;

    }
}

截图:

Highligted issue

1 个答案:

答案 0 :(得分:1)

问题是,如果未选择该行,则需要重置ViewHolder所选状态。

if(selected) {
    setRowColour();
} else {
    resetColourToDefaults();
}

这非常重要,因为RecyclerView会在您向上和向下滚动时重复使用ViewHolder行。重用的行将保留先前应用于它的格式。