title: Show popup menu for row in recyclerview using databinding
body: <p>I am trying to show a popup menu for the items in my RecyclerView:
  <a href="https://i.stack.imgur.com/Z3AwC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z3AwC.png" alt="enter image description here"></a></p>
  
  <p>All the code samples on how to do this that I found online either use Java or when they in rare cases do use Kotlin, it's done without data binding.</p>
  
  <p>Anyway what I'm trying to achieve is this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ImageButton
      android:id="@+id/options"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="14dp"
      android:layout_marginEnd="16dp"
      android:background="?attr/selectableItemBackground"
      android:contentDescription="@string/desc_options"
      android:onClickListener="@{(v) -&gt; holder.test1.invoke()}"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:srcCompat="@drawable/ic_more_vert" /&gt;
  </code></pre>
  
  <p>And in my RowHolder I have the following code:</p>
  
  <pre><code>class AgendaRowHolder(
      private val binding: AgendaRowBinding,
      val onRowClick: (AgendaModel) -&gt; Unit
  ) : RecyclerView.ViewHolder(binding.root) {
      fun bind(model: AgendaModel) {
          binding.model = model
          binding.holder = this
          binding.executePendingBindings()
      }
  
      fun test1() {
          // do something here
      }
  }
  </code></pre>
  
  <p>However this will not compile at all and a workaround to solving this problem is to have a function as a property of the <code>AgendaRowHolder</code> class, but this approach doesn't work since I also have to pass the <code>ImageView</code> for the popup menu to know where to show up. So I used this alternative approach, namely adding an <code>onClickListener</code> in the <code>bind()</code> function:</p>
  
  <pre><code>fun bind(model: AgendaModel) {
      binding.model = model
      binding.holder = this
      binding.executePendingBindings()
  
      binding.options.setOnClickListener { showPopup(it) }
  }
  
  private fun showPopup(view : View) {
      val popup = PopupMenu(view.context, view)
      popup.setOnMenuItemClickListener { item -&gt;
          when (item.itemId) {
              R.id.delete -&gt; {
                  AgendaRepository.delete(binding.model!!)
                  true
              } else -&gt; false
          }
      }
      val inflater: MenuInflater = popup.menuInflater
      inflater.inflate(R.menu.actions_agenda, popup.menu)
      popup.show()
  }
  </code></pre>
  
  <p>This approach "works" in that it will show a popup menu and allow me to delete items, but I have a new problem here in that when an item/row gets delete it doesn't show up until the view is recreated, right now I'm not observing my data so that might be the reason as to why, but what I'm wondering is what would be the best approach to achieve this? Note: another thing that I tried is adding an <code>android:onClick</code> attribute in the xml and trying to handle this in the <code>MainActivity</code>, but I'm not sure which approach to take here. Can the first one be achieved? Ideally, I'd have:</p>
  
  <p><code>android:onClickListener="@{(v) -&gt; holder.showPopup.invoke(model, v)}"</code> and then that <code>showPopup</code> function in the <code>ViewHolder</code>.</p>