Android Data Binding - View Link

I am using the android data binding library in my new application. I'm currently trying to pass another view reference to a method.

I have an ImageButton with onClickListener . In this onClick listener, I want to pass the root view reference to the method.

 <RelativLayout android:id="@+id/root_element" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:contentDescription="@string/close_dialog" android:src="@drawable/ic_close_212121_24dp" android:background="@android:color/transparent" android:onClick="@{() -> Helper.doSth(root_element)}"/> </RelativLayout> 

This source code above is just an example, not a complete one. There are more children, and the image button is not a direct child of the root element. But I think the meaning is clear.

I already tried to pass the link by specifying the identifier of the root view (see above). But that does not work. If I try to compile this, I get an error that the root_element type root_element not specified.

I also tried to import the generated binding class and access the root element with a public field in it. Also, this method does not work, since you must first create a binding class.

So, is there a way to pass a reference to a method? I know that I can pass the identifier of the root view using @id/root_element , but I do not want this, since I need to find a way to get a link to this view with only this identifier.

+7
android data-binding android-view
source share
2 answers

The difference between what you have and what you should do, do not pass id root_element . Rather, pass the view as another variable to the layout file.

In my case, I had a switch in my layout that I wanted to pass as a parameter to a method in my lambda. My code is as follows:

 MyLayoutBinding binding = DataBindingUtil.inflate(inflater, R.layout.my_layout, parent, true); binding.setDataUpdater(mDataUpdater); binding.setTheSwitch(binding.switchFavorite); 

Then my layout looks like this:

 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="dataUpdater" type="..."/> <variable name="theSwitch" type="android.widget.Switch"/> <import type="android.view.View"/> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@{()->dataUpdater.doSomething(theSwitch)}"> <Switch style="@style/Switch" android:id="@+id/switch_favorite" ... /> .../> 

So, as you can see, in my code I get a link to my switch and pass it as a variable in the binding. Then in my layout I have access to it to pass it in my lambda.

+4
source share

You can use root_element, but Android data binding is bound to names. So root_element becomes rootElement. Your handler should be:

 android:onClick="@{() -> Helper.doSth(rootElement)}" 
+34
source share

All Articles