首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用android DataBinding库如何将参数传递给绑定事件

使用android DataBinding库如何将参数传递给绑定事件
EN

Stack Overflow用户
提问于 2016-06-15 07:07:30
回答 1查看 5.5K关注 0票数 6

我学习了Android开发者绑定事件的例子,并逐步实现。效果很好。但是我想将参数从适配器发送到处理程序,如何使用数据绑定处理程序来实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-20 10:08:31

我知道答案了。在xml中,单击onclick使用lambda表达式

代码语言:javascript
复制
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)}"

代码语言:javascript
复制
    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>

然后创建处理程序类,

代码语言:javascript
复制
public class MyHandlers {

public void onItemClicked(View v, Movie movie) {
    Context context = v.getContext();
    context.startActivity(DetailActivity.buildIntent(context, movie));
}

}

然后,您需要将处理程序设置为

代码语言:javascript
复制
binding.setHandler(new MyHandlers());

您还可以将处理程序方法放入任何类中。在这种情况下,必须将类名设置为处理程序。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37828354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档