You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Data Binding Library.md
+176Lines changed: 176 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -623,6 +623,182 @@ private static class User extends BaseObservable {
623
623
* A little work is involved in creating Observable classes, so developers who want to save time or have few properties may use ObservableField and its siblings ObservableBoolean, ObservableByte, ObservableChar, ObservableShort, ObservableInt, ObservableLong, ObservableFloat, ObservableDouble, and ObservableParcelable. ObservableFields are self-contained observable objects that have a single field. The primitive versions avoid boxing and unboxing during access operations. To use, create a public final field in the data class:
624
624
* 在创建Observable类时还是要做一些工作的,所以一些想去解约事件或者希望有一些可使用的属性的开发着可以使用ObservableField和与之类似的ObservableBoolean,ObservableByte,ObservableChar,ObservableDouble,ObservableLong,ObservableFloat,ObservableDouble,ObservableParcelable。ObservableFields 是一个有单一字段的自包含的observable对象。最初的版本是避免在操作时做装箱和拆箱操作。在数据类中以public final 字段形式创建并使用。
625
625
626
+
```
627
+
private static class User {
628
+
public final ObservableField<String> firstName =
629
+
new ObservableField<>();
630
+
public final ObservableField<String> lastName =
631
+
new ObservableField<>();
632
+
public final ObservableInt age = new ObservableInt();
633
+
}
634
+
```
635
+
* That's it! To access the value, use the set and get accessor methods:
636
+
* 就是这样,使用set和get方法去获取值
637
+
638
+
```
639
+
user.firstName.set("Google");
640
+
int age = user.age.get();
641
+
642
+
```
643
+
# Observable Collections
644
+
645
+
* Some applications use more dynamic structures to hold data. Observable collections allow keyed access to these data objects. ObservableArrayMap is useful when the key is a reference type, such as String.
* The generated binding class links the layout variables with the Views within the layout. As discussed earlier, the name and package of the Binding may be customized. The Generated binding classes all extend ViewDataBinding.
* The binding should be created soon after inflation to ensure that the View hierarchy is not disturbed prior to binding to the Views with expressions within the layout. There are a few ways to bind to a layout. The most common is to use the static methods on the Binding class.The inflate method inflates the View hierarchy and binds to it all it one step. There is a simpler version that only takes a LayoutInflater and one that takes a ViewGroup as well:
* A public final field will be generated for each View with an ID in the layout. The binding does a single pass on the View hierarchy, extracting the Views with IDs. This mechanism can be faster than calling findViewById for several Views. For example:
0 commit comments