chari sharma
chari sharma

Reputation: 482

How to initialise click listener on Recycler view adapter using Databinding?

I am going to make an universal adapter for all dynamic layouts , normally i handled all this things but i got stuck that how to initialise click listener using interface so that i define in whatever xml and get event in my class.

i am following this link: https://developer.android.com/topic/libraries/data-binding/index.html

suppose this is my root xml of recycler view:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="handlers" type="com.example.MyHandlers"/>
<variable name="user" type="com.example.User"/>
</data>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"
android:onClick="@{handlers::onClickFriend}"
//>>> i want to initialise interface for position/view instead of @{handlers::onClickFriend}.
  />
</LinearLayout>
</layout>

Please give me link and solution , i will be thankful to you.

Upvotes: 4

Views: 1953

Answers (1)

Ravi
Ravi

Reputation: 35559

You can pass either user/position. If you want to pass position inside clickListener you must have to pass it as variable in xml same as user, and then

android:onClick="@{() -> handlers.onClickFriend(user)}

Or

<variable name="position" type="Integer"/>

and then

android:onClick="@{() -> handlers.onClickFriend(position)}

Upvotes: 1

Related Questions