0
ユーザーがクリックすると、私はrecyclerviewでonmyのアイテムを強調表示しようとしています。しかし、私は予想外の動作をしています。recyclerviewで選択した単一のアイテムandroid
1つのアイテムをクリックすると、アダプターは選択され、強調表示されていることを記録します。しかし、スクロールすると、他の項目が強調表示されていることがわかります。
この問題に関するお手伝いは本当に感謝しています。
マイコード:
マイアダプタ
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;
}
}
スクリーンショット:
はい、それはアダプタのonClick()メソッドの最後のものです。 選択した古いものをリセットし、新しいものを強調表示します。 – Coccoonx
これはonClickメソッドでは役に立ちません。それは 'onBindViewHolder'メソッド自体にある必要があります。 – Knossos