1

I got a parent layout, activity_main.xml, with multiple views inside it, one of which is the RecyclerView view.

Within the recycler view, there are buttons that, once the user deals with them, I would like things to happen in the parent layout that is activity_main.xml.

For example, if I press a button on a ViewHolder item inside the child RecyclerView, in some cases, I would like changes to happen in the parent activity_main.xml layout.

However, my problem is that I don't know how to allow for this communication to happen.

What might be the best practices for such cases?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
caveman
  • 422
  • 3
  • 17

1 Answers1

1

You need to create an interface callback or use a higher-order function.

This Stack Overflow answer is an example of an interface callback: How to access component of MainActivity in recycler view to make onclick listener?.

Kotlin Lambda Functions For RecyclerView Adapter Callbacks in Android is for higher-order functions.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Thanks sir. Perfecto . Not sure I _entirely_ understood, but it inspired me to go into my `MainActivity` class, and defined methods to do things there. Then, as I called `RecyclerView.Adapter`, I passed it `this` as an argument. Then, inside `RecyclerView.Adapter`, I stored that as `val mainActivity: MainActivity` (Kotlin syntax). Any Idea what did I just do? Is it any of the methods above? . It works fine now, but I'm worried if there are any long-term hazards that may unfold upon me and people that rely on me. – caveman Sep 09 '22 at 11:35