DataBinding--旧的值

在棉花糖的介绍中 关于旧的值如下:

你可能也需要一些旧的值。在这个例子中,我们有一个 OnLayoutChanged:

@BindingAdapter("android:onLayoutChange")
public static void setOnLayoutChangeListener(View view,
        View.OnLayoutChangeListener oldValue,
        View.OnLayoutChangeListener newValue) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (oldValue != null) {
            view.removeOnLayoutChangeListener(oldValue);
        }
        if (newValue != null) {
            view.addOnLayoutChangeListener(newValue);
        }
    }
}

我们想在我们添加一个新的值的时候,删除一个旧的值,但在这个场景中,我们不知道旧的那个值是什么。我们可以添加新的,这很容易,但我们如何删除旧的呢?嗯,其实你也可以获得这个值,我们会给你的。如果你有这种代码,我们将会知道要把旧的直给你。对于这样的情况,它将在你的内存中,这样很好。每次它改变,比如开始正确的动画,你会想知道它之前是什么。

只要使用这个 API,我们的数据绑定库就会实现你想要的。你只需考虑如何响应这个变化。当然,你也可以使用多属性来实现同样的结果。我们会把值都传给你。

但在依然没说明白 这个功能是如何使用的 又有什么限制

先补全上面的示例

监听的方法已经有了 还缺一个 使用 跟设置

如下(listener)

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="imageUrl"
            type="String" />

        <variable
            name="event"
            type="com.zk.sample.module.binding.BindingEvent" />
        <!--声明要监听的变量-->
        <variable
            name="userImg"
            type="com.zk.sample.module.binding.model.UserImg" />

        <!--声明要监听的变量-->
        <variable
            name="listener"
            type="android.view.View.OnLayoutChangeListener" />
    </data>

            <LinearLayout
                android:id="@+id/ll_event"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:orientation="vertical">

                <Button
                    style="@style/ButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:onClick="@{holder.imageOnClickListener}"
                    android:text="自定义事件(加载图片)" />

              <!--自定义的 属性-->
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:contentDescription="123"
                    android:scaleType="fitCenter"
                    app:error="@{@color/black}"
                    app:imageUrl="@{imageUrl}"
                    app:placeHolder="@{@color/holderColor}"
                    app:testUrl="@{imageUrl}" />


                <Button
                    style="@style/ButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:onClick="@{()->event.setParamsOnClick(userImg)}"
                    android:text="点击直接修改图片网址" />

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:contentDescription="234"
                    android:onClick="@{event.getTestOnClick()}"
                    android:onLayoutChange="@{listener}"
                    android:scaleType="fitCenter"
                    app:error="@{@color/black}"
                    app:imageUrl="@{userImg.url}"
                    app:placeHolder="@{@color/holderColor}"
                    app:testUrl="@{imageUrl}"/>
            </LinearLayout>
</layout>

当listener 发生变化的时候就会触发setOnLayoutChangeListener

运行结果:我们发现这个变化的监听是在改变之前

我们可以使用自定义的属性:testUrl

监听:

//方法名是随意的
@BindingAdapter("testUrl")
public static void testName(View view, String oldValue, String newValue) {
    LogUtil.d(TAG, "load image ->url:" + newValue + " old:" + oldValue);
    if (view != null) {
        LogUtil.d(TAG, "des = " + view.getContentDescription());
    }
}

此时当imageUrl 发生变化的时候,也就是testUrl 发生变化 就会调用方法

未解:

改为imageUrl是无效的 
@BindingAdapter("imageUrl")
public static void testName(View view, String oldValue, String newValue) {
    LogUtil.d(TAG, "load image ->url:" + newValue + " old:" + oldValue);
    if (view != null) {
        LogUtil.d(TAG, "des = " + view.getContentDescription());
    }
}

附上Demo :

https://github.com/zhaokai1033/LearnProject

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容