I followed examples from Android Developers Binding Events and implementing step-by-step. That working fine. But I want to send parameters from adapter to handlers, how can achieve this using data binding handlers
I got the answer. In xml for onclick use lambda expression
layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="embitel.com.databindingexample.helper.Movie" />
<variable
name="handler"
type="embitel.com.databindingexample.helper.MyHandlers" />
</data>
<android.support.v7.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:onClick="@{(view)->handler.onItemClicked(view,movie)}"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="128dp"
android:scaleType="centerCrop"
app:error="@{@drawable/ic_launcher}"
app:imageUrl="@{movie.imageUrl}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{movie.title}" />
</LinearLayout>
</android.support.v7.widget.CardView>
then create handler class as,
public class MyHandlers {
public void onItemClicked(View v, Movie movie) {
Context context = v.getContext();
context.startActivity(DetailActivity.buildIntent(context, movie));
}
}
then you need to set handler where that xml is iflated as,
binding.setHandler(new MyHandlers());
you can also put handler method in any class. In that case you have to set that class name as handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With