Skip to content

Commit 94df263

Browse files
committed
09301333
1 parent f4cc0ea commit 94df263

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

Data Binding Library.md

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,182 @@ private static class User extends BaseObservable {
623623
* 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:
624624
* 在创建Observable类时还是要做一些工作的,所以一些想去解约事件或者希望有一些可使用的属性的开发着可以使用ObservableField和与之类似的ObservableBoolean,ObservableByte,ObservableChar,ObservableDouble,ObservableLong,ObservableFloat,ObservableDouble,ObservableParcelable。ObservableFields 是一个有单一字段的自包含的observable对象。最初的版本是避免在操作时做装箱和拆箱操作。在数据类中以public final 字段形式创建并使用。
625625

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.
646+
647+
* 一些应用更多地使用动态结构去控制数据。Observable collections 允许键值对的形势去获取数据对象。ObservableArrayMap是很常用的一个,它的key是一个引用类型,比如String。
648+
649+
```
650+
ObservableArrayMap<String, Object> user = new ObservableArrayMap<>();
651+
user.put("firstName", "Google");
652+
user.put("lastName", "Inc.");
653+
user.put("age", 17);
654+
655+
```
656+
* In the layout, the map may be accessed through the String keys:
657+
658+
* 在布局中,可以通过String类型的key访问map。
659+
660+
```
661+
<data>
662+
<import type="android.databinding.ObservableMap"/>
663+
<variable name="user" type="ObservableMap&lt;String, Object&gt;"/>
664+
</data>
665+
666+
<TextView
667+
android:text='@{user["lastName"]}'
668+
android:layout_width="wrap_content"
669+
android:layout_height="wrap_content"/>
670+
<TextView
671+
android:text='@{String.valueOf(1 + (Integer)user["age"])}'
672+
android:layout_width="wrap_content"
673+
android:layout_height="wrap_content"/>
674+
```
675+
* ObservableArrayList is useful when the key is an integer:
676+
677+
* ObservableArrayList 也是很常用的,它的key是一个integer:
678+
```
679+
ObservableArrayList<Object> user = new ObservableArrayList<>();
680+
user.add("Google");
681+
user.add("Inc.");
682+
user.add(17);
683+
```
684+
* In the layout, the list may be accessed through the indices:
685+
686+
* 在布局中可以通过 下标访问list
687+
688+
```
689+
<data>
690+
<import type="android.databinding.ObservableList"/>
691+
<import type="com.example.my.app.Fields"/>
692+
<variable name="user" type="ObservableList&lt;Object&gt;"/>
693+
</data>
694+
695+
<TextView
696+
android:text='@{user[Fields.LAST_NAME]}'
697+
android:layout_width="wrap_content"
698+
android:layout_height="wrap_content"/>
699+
<TextView
700+
android:text='@{String.valueOf(1 + (Integer)user[Fields.AGE])}'
701+
android:layout_width="wrap_content"
702+
android:layout_height="wrap_content"/>
703+
```
704+
# Generated Binding
705+
* 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.
706+
707+
* 使用Views内部的layout可以将生成的绑定类于布局变量链接起来。就像前面讨论的一样,绑定的包名可疑被自定义。所有生成的绑定类都继承ViewDataBinding这个类
708+
709+
### Creating
710+
* 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:
711+
* 在View被inflation后应该尽快创建绑定,这样能确保在layout里用表达式绑定View之前View的层级不被干扰。以下是几种绑定layout的方式。最常用的是在绑定类上使用静态方法inflate方法去Inflates View的层级和绑定它,所有操作只需要一步完成。这是i 个简单版本的方法,里面只使用一噶LayoutInflater和一个ViewGroup:
712+
713+
```
714+
MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater);
715+
MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false);
716+
```
717+
* If the layout was inflated using a different mechanism, it may be bound separately:
718+
719+
* 如果layout是通过不同的机制进行inflated,那么它可以被单独地绑定。
720+
```
721+
MyLayoutBinding binding = MyLayoutBinding.bind(viewRoot);
722+
```
723+
* Sometimes the binding cannot be known in advance. In such cases, the binding can be created using the DataBindingUtil class:
724+
725+
* 有时,我们并不能预先知道绑定的东西。在这种情况下,可以使用DataBindingUtils类去创建绑定。
726+
727+
```
728+
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,
729+
parent, attachToParent);
730+
ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId);
731+
```
732+
# Views With IDs
733+
734+
* 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:
735+
736+
* 在layout中使用View的一个ID为每个View生成一个public final类型的字段。在View的层级中,绑定根据IDs提取View并简单的传入View的层级中。这个机制比通过调用各种Views的findViewById方法获取View更加的快速。例如:
737+
738+
```
739+
<layout xmlns:android="http://schemas.android.com/apk/res/android">
740+
<data>
741+
<variable name="user" type="com.example.User"/>
742+
</data>
743+
<LinearLayout
744+
android:orientation="vertical"
745+
android:layout_width="match_parent"
746+
android:layout_height="match_parent">
747+
<TextView android:layout_width="wrap_content"
748+
android:layout_height="wrap_content"
749+
android:text="@{user.firstName}"
750+
android:id="@+id/firstName"/>
751+
<TextView android:layout_width="wrap_content"
752+
android:layout_height="wrap_content"
753+
android:text="@{user.lastName}"
754+
android:id="@+id/lastName"/>
755+
</LinearLayout>
756+
</layout>
757+
```
758+
* Will generate a binding class with:
759+
760+
* 将会使用如下代码生成一个绑定类:
761+
762+
```
763+
public final TextView firstName;
764+
public final TextView lastName;
765+
```
766+
* IDs are not nearly as necessary as without data binding, but there are still some instances where access to Views are still necessary from code.
767+
768+
* 如果没有数据绑定,IDs并不是必须的,但是仍然有些情况下需要从代码里面访问Views时,IDs仍然是需要的。
769+
770+
# Variables 变量
771+
772+
* Each variable will be given accessor methods.
773+
* 每一个变量都将被给予一个存储器方法
774+
```
775+
<data>
776+
<import type="android.graphics.drawable.Drawable"/>
777+
<variable name="user" type="com.example.User"/>
778+
<variable name="image" type="Drawable"/>
779+
<variable name="note" type="String"/>
780+
</data>
781+
782+
```
783+
* will generate setters and getters in the binding:
784+
* 在绑定时将会生成一个setters和一个getters存储器
785+
786+
```
787+
public abstract com.example.User getUser();
788+
public abstract void setUser(com.example.User user);
789+
public abstract Drawable getImage();
790+
public abstract void setImage(Drawable image);
791+
public abstract String getNote();
792+
public abstract void setNote(String note);
793+
```
794+
# ViewStubs
795+
796+
797+
798+
799+
800+
801+
626802

627803

628804

0 commit comments

Comments
 (0)