siavash hosseini
siavash hosseini

Reputation: 5

how to change specific items in recycler view?

I want to remove textview from specific items in recycler view. I wrote the below code to do that

@Override
    public void onBindViewHolder(@NonNull final Myholder holder, final int possion) {
        final String n = names.get(possion);
        if(possion==3){
        holder.textView.setVisibility(View.GONE);}}

but this changes all items and i want to make gone only position number 3 item view. this is my view holder

public static class Myholder extends RecyclerView.ViewHolder {
        TextView textView;
        public Myholder(@NonNull View itemView) {
            super(itemView);
        textView = itemView.findViewById(R.id.textfolder);
        }
    }

what should i do?

Upvotes: 0

Views: 928

Answers (1)

Ben P.
Ben P.

Reputation: 54214

Always remember that RecyclerView recycles view holders. That is, the same ViewHolder instance will be re-used for different views. This means that it is almost always a bad idea to have an if statement that modifies a view withouth having a corresponding else.

So, try this instead:

if (possion == 3) {
    holder.textView.setVisibility(View.GONE);
} else {
    holder.textView.setVisibility(View.VISIBLE);
}

Note also that just checking the position argument is not necessarily a good idea. If you use notifyItemInserted() or notifyItemRemoved(), this can lead to problems.

It would be better to set something about the item at that position to indicate that its text should not be shown.

Upvotes: 3

Related Questions