Skip to content Skip to sidebar Skip to footer

Hide/show Buttons Inside Recyclerview

I want to show and hide some buttons within the recycler view in recycler item clicked. For example I have a recyclerw view with two items like this On click of the 1st item, the

Solution 1:

Set an adapter variable:

privateint currentSelectedPosition = RecyclerView.NO_POSITION

Change your personCardView adapter layout to have both the buttons in them, and set their visibility to GONE. Reference them in your ViewHolder (e.g. Button editButton, ButtondeleteButton)

In your item onClickListener, set the currentPosition and call notifyDataSetChanged() - this is necessary to re-hide the buttons in previous selections and show the buttons in this selection. Then in onBindViewHolder, apply the VISIBLE or GONE logic as below. I personally set the itemClickListener inside onBindViewHolder too, so the whole method would look like this:

publicvoidOnBindViewHolder(RecyclerView.ViewHolder holder, int position)  {
        holder.itemView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
               currentSelectedPosition = position;
               notifyDataSetChanged();
            }
        });
        if (currentSelectedPosition == position) {
             holder.editButton.setVisibility(View.VISIBLE);
             holder.editButton.setOnClickListener(newView.OnClickListener() {
                  @OverridepublicvoidonClick(View view) {
                       // your edit button click event here
                  }
             });
             holder.deleteButton.setVisibility(View.VISIBLE);
             holder.deleteButton.setOnClickListener(newView.OnClickListener() {
                  @OverridepublicvoidonClick(View view) {
                     // your delete button click event here
                  }
             });
        } else {
             holder.editButton.setVisibility(View.GONE);
             holder.deleteButton.setVisibility(View.GONE);
        }
      //..... the rest of your code for onBindViewHolder (updating your text views and so on)
    }

Post a Comment for "Hide/show Buttons Inside Recyclerview"