4

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.

chari sharma
  • 462
  • 2
  • 16
  • This will point you in the right direction: https://stackoverflow.com/a/42795719/436417 – Uli Nov 10 '17 at 14:42
  • i am not possible to get : binding.setViewModel(new ViewModel()); i mention view model in xml also but i am getting only binding.setVariable . here is my xml: – chari sharma Nov 15 '17 at 12:46
  • 1
    finally i solve it using this holder.getBinding().setVariable(com.package.BR.viewHolder, holder); – chari sharma Nov 15 '17 at 13:18
  • You may be using the wrong "ViewBinding" class in the ViewHolder. Use the binding class that is generated from the xml file that you just quoted. – Uli Nov 15 '17 at 13:33

1 Answers1

1

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)}
Ravi
  • 34,851
  • 21
  • 122
  • 183