android -------- Data Binding的使用(一)

來源:互聯網
上載者:User

標籤:實體類   string   util   代碼   包名   binding   summary   資料繫結   xtend   

Google推出自己官方的資料繫結架構Data Binding Library 已經很久了,很多企業也在使用

面試的時候也有問到,所以也去學習了一番,特來分享一下,希望對各位有所協助

 

描述:

Data Binding 是把資料直接綁定到 XML 檔案上,並能實現自動重新整理。

Data Binding 減少了代碼的耦合性,一些如 findViewById、setText 之類的操作都可以通過綁定實現。

Data Binding 是MVVM模式開發的

 

Google 官方文檔:https://developer.android.google.cn/reference/android/databinding/package-summary

 

配置:

環境要求:

  1. 系統版本:Android 2.1(API level 7)及以上

  2. Gradle版本:1.5.0-alpha1及以上

  3. Android Studio版本:1.3及以上

 

在app根目錄的build.gradle檔案中加入Data Binding配置:

android {
compileSdkVersion 22
buildToolsVersion "27.0.3"

dataBinding {
enabled = true
}
.....
}

 

簡單的一個案例 :布局中綁定資料

先寫一個實體類

public class Employee {    public Employee(String frstName,String lastName){        this.frstName = frstName;        this.lastName = lastName;    }    private String frstName;    private String lastName;    public String getFrstName() {        return frstName;    }    public void setFrstName(String frstName) {        this.frstName = frstName;    }    public String getLastName() {        return lastName;    }    public void setLastName(String lastName) {        this.lastName = lastName;    }}

 

布局檔案 activity_main.xml

<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:bind="http://schemas.android.com/apk/res-auto">        <data>        <variable            name="employee"            type="com.zhangqie.databinding.demo1.Employee"/><!-- 實體類的地址 -->    </data>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        >        <TextView            android:id="@+id/tv_name1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>        <TextView            android:id="@+id/tv_name2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"             />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@{employee.frstName}"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@{employee.lastName}"            />    </LinearLayout></layout>

 

由上面可看出

我們在最外層再套一層 標籤layout,裡麵包含了 data標籤和LinearLayout標籤

data 標籤下面的 variable 定義資料繫結用的實體類

type 裡面是完整的帶包名的類,
name 自訂一個名稱,下面具體綁定的時候就是用的這個名稱。

layout標籤的定義會預設產生一個資料繫結類 (類名根據檔案的名稱來產生)

 

編譯一下我就會產生了一個 ActivityMainBinding的類了

 

之後MainActivity中

 

public class MainActivity extends AppCompatActivity {    Employee employee = new Employee("切切歆語","小張");    ActivityMainBinding binding;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);       // setContentView(R.layout.activity_main);        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);        //直接存取Id 設定屬性        binding.tvName1.setText("小張");        binding.tvName2.setText("歆語");        //傳對象綁定資料        //兩種方式一樣        binding.setEmployee(employee);        //binding.setVariable(com.zhangqie.databinding.BR.employee,employee);    }
}

 

這樣看起來是不是方便了很多呢

 

運行效果:

              

 

特點:

MVVM 的實現

提高開發效率

效能高 ,功能強

 

上面是Data Binding的基本用法,當然還有其它很多的進階用法,更新中...

 

android -------- Data Binding的使用(一)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.