1. Implicit Data Binding & Explicit Data Binding
//The main difference between implicit and explicit is that implicit data binding is //done at run time while explicit data binding is done at compile time
1). Implicit Data Binding:
Eg1.
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init(15)" minWidth="955" minHeight="600"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import components.ClassA; import mx.controls.Alert; import mx.events.PropertyChangeEvent; private var classA:ClassA = new ClassA(); private function init(num:Number):void { classA.addEventListener("propertyChange", handler); classA.value = num; } private function handler(event:PropertyChangeEvent):void { Alert.show("New value: " + event.newValue + ", Old value: " + event.oldValue); } ]]> </fx:Script> </s:Application>
package components { import mx.core.UIComponent; public class ClassA extends UIComponent { private var _value:Number; [Bindable] public function get value():Number { return _value; } public function set value(num:Number):void { _value = num; } } }
Eg2.
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init(23);" minWidth="955" minHeight="600"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Metadata> [Event(name="valueChanged", type="event.ValueChangedEvent")] </fx:Metadata> <fx:Script> <![CDATA[ import components.ClassB; import event.ValueChangedEvent; import mx.controls.Alert; private var classB:ClassB = new ClassB(); private function init(num:Number):void { classB.addEventListener("valueChanged", handler); classB.val = num; } private function handler(e:ValueChangedEvent):void { Alert.show("New value: " + e.expVal + ", Old value: " + e.preVal); } ]]> </fx:Script> </s:Application>
package components { import event.ValueChangedEvent; import flash.events.Event; import mx.core.UIComponent; public class ClassB extends UIComponent { private var _val:Number; [Bindable(event="valueChanged")] public function get val():Number { return _val; } public function set val(num:Number):void { var eventObj:ValueChangedEvent = new ValueChangedEvent("valueChanged", new String(_val), new String(num)); _val = num; dispatchEvent(eventObj); } } }
package event { import flash.events.Event; public class ValueChangedEvent extends Event { public var preVal:String; public var expVal:String; public function ValueChangedEvent(type:String, preVal:String, expVal:String) { super(type); this.preVal = preVal; this.expVal = expVal; } } }
Comment:
1) In Eg1: Once a variable is changed, you'll get notification (system will dispatch PropertyChange event automatically). The [Bindable] is compiled as [Bindable(event="propertyChange")].
2) In Eg2: We dispatch our custom event every time property changed and catch our custom event instead of the event dispatched by system.
2). Explicit Data Binding
1. Reasons why we use Explicit Data Binding:
1) You want to cast an object type with another object type.
2) You want to avoid compile-time errors in a mismatch between objects.
3) You are dealing with forms where all the properties are of type String, but they need to be
converted to another format.
Eg.
<s:Button width="50" label="{num.toString()}" click="num++" />
var com:UIComponent = object as UIComponent
trace(Number("2"));
P.S
1. Flex SDK 3.4 and up brings a new data type called Vector, which we encourage using when you need to create a collection of a data type. But it enforces every element in it must be the same type. Just like Generic in JAVA.
Eg.
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="initApp(event);" minWidth="955" minHeight="600"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import vo.Student; private var studentList:Vector.<Student>; private function initApp(event:Event):void { var student1:Student = new Student(1, "Davy", "male"); var student2:Student = new Student(2, "Caly", "female"); studentList = Vector.<Student>([student1, student2]); } ]]> </fx:Script> </s:Application>
package vo { public class Student { public var id:int; public var name:String; public var gender:String; public function Student(id:int, name:String, gender:String) { this.id = id; this.name = name; this.gender = gender; } } }
相关推荐
Android Data Binding RecyclerView Using Recyclerview with the new Android Data Binding framework. ... dataBinding { enabled = true } Modify your layout Remember to use your classes and packages ;-).
DataBinding-ktx使声明数据绑定变得容易。 在这里。 数据绑定中的问题 忘记在DataBinding中调用setLifecycleOwner 。 在Activity和Fragment中声明binding变量的方式有所不同。 在活动中,可以使用by lazy声明...
数据绑定(DataBinding)是Android开发中的一个库,它提供了在布局XML文件中直接与Java对象绑定的能力,简化了UI和业务逻辑之间的交互。通过DataBinding,开发者可以更直观地处理用户界面的数据更新,减少代码量,...
**DataBinding 概述** DataBinding 是 Android 开发中的一个库,它允许开发者将数据模型直接绑定到用户界面,从而简化了代码,提高了可读性和可维护性。这个框架是 Android 支持库的一部分,引入 DataBinding 可以...
DataBinding RecyclerViewAdapter 中文版 README Free from writing adapters! NO MORE CLASSES! Super simple RecyclerView adapter using Data Binding Technology, no longer need to write any adapter! You don...
:dna:Android DataBinding套件,用于通知MVVM架构上从Model层到UI层的数据更改。 Bindables:dna:Android DataBinding工具包,用于通知从模型层到UI层的数据更改。 该库提供了DataBinding的基类(BindingActivity,...
以下是一个简单的例子,展示如何在布局中使用DataBinding: ```xml <layout xmlns:android="http://schemas.android.com/apk/res/android"> name="user" type="com.example.User" /> ...> android:...
赠送jar包:cxf-rt-databinding-jaxb-3.0.1.jar; 赠送原API文档:cxf-rt-databinding-jaxb-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-databinding-jaxb-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-...
总结起来,"Android支持DataBinding的RecyclerView通用Adapter"是一个高效的解决方案,它整合了`DataBinding`的便利性和`BRVAH`的实用性,为开发者提供了一个强大且易用的工具,帮助他们更好地管理和展示列表数据。...
在Android开发中,DataBinding是一种强大的工具,它简化了UI与数据模型之间的交互,使得代码更加清晰、可读性更强。这个"DataBindingDemo2"是基于MVVM(Model-View-ViewModel)架构模式的一个示例,展示了如何在实际...
《Android DataBinding 深入解析与实战指南》 在Android开发中,DataBinding库提供了一种强大的方式来简化UI和数据模型之间的交互,极大地提高了代码的可读性和可维护性。本文将深入探讨DataBinding的功能、优势...
在Android开发中,DataBinding库是Google推荐的一种用于简化视图和数据模型之间绑定的技术,它使得MVVM(Model-View-ViewModel)架构模式在Android应用中的实现更加便捷。本篇文章将深入探讨DataBinding的工作原理、...
在Android开发中,数据绑定(DataBinding)和视图绑定(ViewBinding)是两种非常重要的技术,它们可以显著提升代码的可读性和可维护性,减少样板代码,并增强UI组件与业务逻辑之间的交互。本项目提供了简单的API实现...
:dna: Android DataBinding工具包,用于通知数据从模型层到UI层的更改。 该库提供了DataBinding的基类(BindingActivity,BindingFragment,BindingViewModel), 并支持在没有可观察字段和LiveData的情况下通知...
Android开发:10.DataBinding数据绑定技术解析.pdf
在Android开发中,DataBinding库是一个非常重要的组件,它能够帮助开发者更加简洁、高效地处理UI与数据之间的绑定。这个名为"Android-一些DataBinding相关的工具类"的资源集,显然是为了提供一些辅助方法和扩展,以...
在Android开发中,DataBinding库是Google推荐的一种用于简化视图和数据绑定的工具,它极大地减少了我们在Activity或Fragment中处理UI逻辑的代码量。ViewStub是一个轻量级的视图,它在默认情况下不可见,只有在被...
### DataBinding2使用手册 #### 一、简介 DataBinding2是基于JQuery的一个扩展库,主要用于简化HTML元素与后端数据之间的绑定过程。相比于早期版本,DataBinding2增加了许多新特性,使得开发者能够更加高效地处理...
即时天气 :sun_behind_cloud: 使用 MVVM 模式、Retrofit2、Dagger2、LiveData、ViewModel、Coroutines、Room、Navigation Components、Data Binding 和来自一些其他库实现的 Android 天气应用程序。...