Android_Binding 笔记二

上述提到的Observable 和Command 定义在model中。在Activity初始化时 传递给Binder的构造。

A Model need to have basically two types of public Fields:

  1. Observable
  2. Command

Observable<?> is a Generic type that denotes something within it is Observable, which changes to it will fire a notification to all its subscribers.

数据变更时,能及时刷新相应的调用界面。

Command is an interface that defines the behavior, a command is normally bind to View actions, such as click on a button or selection of a spinner.

定义用户交互行为。

 

BindingListOfObjects  这里说明下 如何 绑定 List of Objects.


AB 有能力对Arrays与Cursor数据进行绑定。绑定Arrays 可以按 全双向绑定( full two-way binding)方式,即普通的View Model 处理。

但是对于Cursor 有些不同。

For all subclasses of Adapter View(including ListView, Spinner etc), it supports Binding to list of objects.为了绑定,需要一对 标记去实现。

binding:itemSource="MusicList"
binding:itemTemplate="@layout/music_row"
itemSource is the 'List' to bind to. It accepts ObservableCollections and CursorObservables. (It also accepts any Observable<adapter>

, in such case, the templates will be ignored) 

itemSource :可以是ObservableCollections 或者  CursorObservables。(亦可 任何 Observable<adapter>,此时可以不需要 templates 对象)
itemTemplate is the layout that each item will display with. All binding syntax can be used there (even binding with Command)

itemTemplate :当中可以有逻辑判断包括 Converters 逻辑 或者 Command 方法。
For spinners, the spinnerTemplate attribute is also supported. When working with spinner, the spinnerTemplate is the 'normal' template when spinner is not active, while itemTemplate in this case maps to drop-down layout.

针对 Spinners控件,支持 binding:spinnerTemplate 属性。当Spinner默认情况下,显示的正式spinnerTemplate 对应的布局。下拉时显示的是itemTemplate的布局

Converters supported in itemSource

Since itemSource accepts Observable<adapters> as parameter, converters can be used to more sophisticated control on appearances on ListViews.Up to version 0.2, two converters for itemSource is provided:

ADAPTER ADAPTER accepts only one argument, and it must be DynamicObject.It will return a simple adapter, 
the same parameters to gueei.binding.collections.Utility.getSimpleAdapter
STITCH multiple adapters to one adapter for used in List Views

itemSource 除了常用的Observable<adapters>作为参数,同时可以使用 converters 定义的工具类。 新增:ADAPTER、STITCH(现在应该不只这些)。还有SECTION...

  ADAPTER({
      template=@layout/LAYOUT_ID, 
      source=SOURCE_OF_COLLECTION, 
      spinnerTemplate=@layout/LAYOUT_ID })

以上方式替代  binding:itemTemplate / binding:spinnerTemplate / binding:itemSource    spinnerTemplate 是可选项。

 

The ADAPTER by itself do nothing more than separating markups to different part, but it is a base on more complicated case, for example:

ADAPTER 在特定情况下,只作为 分离的标记使用,例如:

  STITCH(
      ADAPTER({template=@layout/list_item, source=CURSOROBSERVABLE}),
      ADAPTER({template=@layout/list_item2, source=ARRAYLISTOBSERVABLE})
    )

把两个List 的内容 放在一个ListView中展示。

 展示一个更复杂些的:这里用到了SECTION

<ListView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        binding:onItemClicked="ToastItem"
        binding:clickedItem="ClickedItem"
        binding:itemSource="STITCH(
                   SECTION('Section A: From Array List', @layout/section_name),
                   ADAPTER({source=SectionAList, template=@layout/list_item}),
                   SECTION('Section B: Also from Array List', @layout/section_name),
                   ADAPTER({source=SectionBList, template=ALT_TEMPLATE(@layout/list_item, @layout/list_item2)}),
                SECTION('Section C: From another Array List', @layout/section_name),
                ADAPTER({source=SectionCList, template=@layout/list_item})
            )"
    />

SECTION: Returns a Single Item Adapter (SingletonAdapter) for the given object.

This is most useful as declaring header/footer templates for list views, or as divider to divide list in sections

返回Adapter的一项item。经常用来 声明ListView的 header/footer 内容。或者 将list 分成片段。

posted @ 2013-09-03 15:50  pinotao  阅读(435)  评论(0编辑  收藏  举报