我学习了Android开发者绑定事件的例子,并逐步实现。效果很好。但是我想将参数从适配器发送到处理程序,如何使用数据绑定处理程序来实现这一点?
发布于 2016-06-20 10:08:31
我知道答案了。在xml中,单击onclick使用lambda表达式
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>
然后创建处理程序类,
public class MyHandlers {
public void onItemClicked(View v, Movie movie) {
Context context = v.getContext();
context.startActivity(DetailActivity.buildIntent(context, movie));
}}
然后,您需要将处理程序设置为
binding.setHandler(new MyHandlers());您还可以将处理程序方法放入任何类中。在这种情况下,必须将类名设置为处理程序。
https://stackoverflow.com/questions/37828354
复制相似问题