问题
I want to make only one item of the viewholder non recyclable and not the whole viewholder. How can I do so?
I dont want the btnSave
to be recyclable since its state may be different for each row.
Here is the code of what I have done so far :
public static class AlertViewHolder extends RecyclerView.ViewHolder {
TextView tvTitle, tvDescription;
Button btnLink, btnSave;
AlertViewHolder(View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
tvDescription = (TextView) itemView.findViewById(R.id.tvDescription);
btnLink = (Button) itemView.findViewById(R.id.btnLink);
btnSave = (Button) itemView.findViewById(R.id.btnSave);
btnLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(alerts.get(getAdapterposition()).getLink()));
context.startActivity(browserIntent);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onSaveClicked(view);
}
});
this.setIsRecyclable(false);
}
void onSaveClicked(View view) {
DbHelper.getInstance(context).addSavedFeedRecord(alerts.get(getAdapterPosition()));
Snackbar.make(view, "Item Saved", Snackbar.LENGTH_LONG)
.setAction("UNDO", null).show();
view.setEnabled(false);
}
}
or the other way is:
@Override
public void onBindViewHolder(AlertViewHolder alertViewHolder, final int i) {
alertViewHolder.setIsRecyclable(false);
alertViewHolder.tvDescription.setText(alerts.get(i).getDesc());
}
回答1:
you can wrap
this.setIsRecyclable(false);
with
if(getposition()=item_position)
来源:https://stackoverflow.com/questions/32901035/how-to-make-a-single-item-of-viewholder-non-recyclable