hhhai0304
hhhai0304

Reputation: 55

Show / Hide buttons in RecyclerView adapter from Activity

I have some RecyclerView item with invisible button and I would like to change all button visibility from Activity. Like this:

Before

After clicked Edit Button in Activity

Please help me.

Upvotes: 2

Views: 5451

Answers (1)

Nilesh Singh
Nilesh Singh

Reputation: 1750

Why don't you create a method inside your RecyclerAdapter which will activate the button when a certain action happens in the Activity. Let's say an activity named activateButtons like this:

public void activateButtons(boolean activate) {
    this.activate = activate; 
    notifyDataSetChanged(); //need to call it for the child views to be re-created with buttons.
}

Now, inside your onBindViewHolder, do something like this:

if (activate) {
   buttons.setVisibility(View.VISIBLE);
} else {
   buttons.setVisibility(View.INVISIBLE);
}

and now, the final step, call the activateButtons method from Activity on an action:

editButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
         adapter.activateButtons(true);  
      }
});

Upvotes: 11

Related Questions