S
S
StynuBlizz2017-06-21 09:36:46
Android
StynuBlizz, 2017-06-21 09:36:46

Why does recyclerview use one viewHolder multiple times? And how, based on this, is it correct to process the click?

There is a list, it has 30 elements and by clicking on the button in the first element, this button should change its background, but the problem is that along with the background of this button, the back of the button in the 15th element also changes. And so for each element, i.e. I click on the button in the 2nd element will change to 16, and so on.

private class SubscriptionHolder extends RecyclerView.ViewHolder {
       private Button buttonAddToFriendSUBSCRIPTIONS_FR;

        SubscriptionHolder (View itemView,int viewType) {
            super(itemView);
            buttonAddToFriendSUBSCRIPTIONS_FR = (Button) itemView.findViewById(R.id.buttonAddToFriendSUBSCRIPTIONS_FR);
            buttonAddToFriendSUBSCRIPTIONS_FR.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {                      
                    buttonAddToFriendSUBSCRIPTIONS_FR.setBackgroundResource(R.drawable.b_all_button_round_ring_color_blue);
                    buttonAddToFriendSUBSCRIPTIONS_FR.setTextColor(getResources().getColor(R.color.textColorBlue));
                    }
                });     
            }
        }

         void setValue (String id,String username){
            fieldUsernameITEM_FOR_SUBSCRIPTION_FR.setText(username);
            user_id=id;
        }
    }

    private class SubscriptionRecyclerAdapter extends RecyclerView.Adapter<SubscriptionHolder>{

        @Override
        public SubscriptionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_for_subscription_fr,parent,false);
                return new SubscriptionHolder(view);
        }
        @Override
        public void onBindViewHolder(final SubscriptionHolder holder, int position) {
                holder.setValue("id","username");
            }
        
        @Override
        public int getItemCount() {
           return array.size()
        }
    }

In principle, the implementation is standard, maybe it's just the adapter's logic, I read somewhere that it creates several identical viewHolder objects. In general, how do I process clicks so that it applies to only one button?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-06-21
@StynuBlizz

Holder is used a few times because that's what it's all about. Wangyu that when you click on the first element, the fifteenth is not yet visible. The views and holder are reused, but the color you set when clicked remains the same.
Conclusion. If you change any property, you should change it whenever possible. In your example, when you bind the holder, you need to set the default color.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question