• Android_binding 笔记一


    前言:接触android_binding 开源框架~ 为了MVVM框架的开发。

    MVVM: 

    MVVM Redefined for Android
    
    Model: Model in Android can be data coming from within your application (including Shared Preferences), Database (in Cursor, or via other Data Access Object) or externally (via Cursor to other Data Contract).
        Model 就是数据源(sp、.db 、file、cursor等等)
    View: All the elements displayed in GUI, that include android.widget.* family, and your custom views.
        View 主要进行展示 GUI _图形用户界面(Graphical User Interface)
    ViewModel: ViewModel exposes properties and commands for the View, it also serves in data binding between the View and the Model. In Android, most of this job is done in Activity.
        ViewModel 提供属性与命令(方法)给View.同时处理 View和Model间的数据绑定(而android SDK里这些操作由Activity承担)

    Movitation:  使用android_binding的动机在于 Activity 的工作量繁重,不仅要处理用户交互,还要控制流,数据层、业务逻辑层的交互。

           而使用android_binding(以下简称 ab)的目的在于,让Activity只处理用户交互。

           {Activity只要负责提供 ViewModel 对象给View 用于渲染结果——描述不准确__

              Activity, it's job is only to supply the ViewModel that the View requires to render the result}

            --- Activity decided the creation of ViewModel and which View is going to render, also manages the life cycle of ViewModel

           ViewModel:是与View无直接耦合的类。用于 解耦视图(用户交互)逻辑和交互。也可用于单元测试。

    The ViewModel is something that bridges the View and Model (it will be the contact provider in this example). The ViewModel in Android Binding is a class defining all the 'bindable' data/command Fields for the View to access. Following shows a portion of the ViewModel:

      

    public class ContactManagerModel {
        private Activity mContext;
        
        public CursorSource<ContactRowModel> ContactList = 
            new CursorSource<ContactRowModel>
                (ContactRowModel.class, new Factory());
        
        public BooleanObservable ShowInvisible = new BooleanObservable(false);
    
        public Command PopulateList = new Command(){
            public void Invoke(View view, Object... args) {
                populateContactList();
            }
        };
        public Command AddContact = new Command(){
            public void Invoke(View view, Object... args) {
                launchContactAdder();
            }
        };
    
        private void populateContactList() {
                // Build adapter with contact entries
                Cursor cursor = getContacts();
                ContactList.setCursor(cursor);
            }

    概要ab的主要特征优势(官网给的):

    • Declare view binding in Layout XML files. No additional files needed.
    • Helps implements MVVM
    • Much easier for Unit-Testing
    • Model Validation that validates against ViewModel
    • Support Cursor results, and you can even validate the cursor result!

    入门框架看的犯困,还是从 类似android的 View先了解下吧。

    首先android_binding 从字面上理解,将原本android的ui api封装了。通过它指定的方式 绑定UI间的 关系。

    下面讲UI:

    1.最基本 就是 属性 对应

    XXXObservable extends  Observable<T>

     例如:定义一个字符串 就 要写成

    private StringObservable mStrOb = new StringObservable();

    2.事件的触发 command 的方式。 有点像 android 绑定控件监听的方式之一的 :

    android api  xml 中 定义的

    <Button
    ....
    android:onClick="register">

      对应java中的 

    private void register(View v){...}

    android_binding api  xml 中定义:

    <Button
    ....
    binding:onClick="register">

     对应java中的

    public final Command register = new Command(){
            public void Invoke(View view, Object... args) {            
               ....
            }
        };

     

    特点:在xml中可以进行逻辑操作。源于 android_binding(以下简称 ab)api中 Converters包提供的一系列的 工具类。

      

    Converters  是一个强大的机制,可以解放 View Model ,让其免于处理繁多的显示逻辑操作。大部分的显示逻辑处理支持在 XML 中声明标记 而把 View Model 作为一个 data-provider(数据提供者)来使用。

    比如:

    <TextView
     ...  
       IF(BooleanCondition,ARGB(0,0,0),ARGB(255,255,255))/>

    IF(Boolean condition_boolean,Object arg_if_true,Object arg_if_false)  

    condition_boolean:判断条件。 arg_if_true:判断条件为true时执行该条件。 arg_if_false:判断条件为false时执行该条件。

     因此用户可以仅通过xml就实现 原本在java代码中需要做的逻辑判断。

  • 相关阅读:
    自我介绍 Self Introduction
    HDU1864 最大报销额
    HDU2955 Robberies
    Sicily 1509. Rails
    Sicily 1031. Campus
    Sicily 1090. Highways
    Sicily 1034. Forest
    Sicily 1800. Sequence
    Sicily 1150. 简单魔板
    CodeVS4919 线段树练习4
  • 原文地址:https://www.cnblogs.com/pinotao/p/3297336.html
Copyright © 2020-2023  润新知