0

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());

}
Anudeep Samaiya
  • 1,700
  • 1
  • 27
  • 31
  • Being rather new to android development, I'm wondering whether one should even try to do this (performance?). Could you tell me about a use case for this problem? – Bö macht Blau Oct 02 '15 at 06:19
  • 1
    http://stackoverflow.com/a/13165506/2079692 – Anudeep Samaiya Oct 02 '15 at 11:51
  • 1
    http://stackoverflow.com/a/19289890/2079692 – Anudeep Samaiya Oct 02 '15 at 11:55
  • thanks for the links about the view holder pattern! But if I understood them, I still wonder: if there's room for five rows on screen, then the system will create maybe seven (?) row views and recycle them as needed on scrolling. So if you have a list with 100 entries (100 different titles), is there not a huge variety of states (=texts) to all the rows? Which of them would I want to keep from recycling, and why? because you said you wanted only the button to be recyclable. – Bö macht Blau Oct 02 '15 at 16:18
  • (my mistake) I dont want button to be recycled, since their state changes for every row – Anudeep Samaiya Oct 02 '15 at 16:32
  • so does the title - more so, in fact – Bö macht Blau Oct 02 '15 at 16:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91194/discussion-between-anudeep-samaiya-and-0x0nosugar). – Anudeep Samaiya Oct 02 '15 at 16:34
  • Need to avoid a single view type recycling,because I adding Adview as a view type in the vertical recyclerview so every time I scroll the Ad view is getting recreated. – RAHULRSANNIDHI Mar 30 '16 at 06:19
  • @AnudeepSamaiya Did you have problems with artifacts (ghosting) when changing the view to setIsRecyclable(false)? I did this for a viewHolder containing two view types and got very strange results. – Lukasz Jun 15 '16 at 22:13
  • @Lukasz No I did not have any such issues. – Anudeep Samaiya Jun 16 '16 at 14:24

1 Answers1

2

you can wrap

this.setIsRecyclable(false);

with

if(getposition()=item_position)

Hanaa Mohamed
  • 107
  • 1
  • 11