Databinding xml例子

 https://juejin.im/entry/59104e43128fe1005857ee5f

一篇文章鏈接

僅僅展示數據綁定成功後xml使用

android:text="@{String.valueOf(index + 1)}"
android:visibility="@{age > 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'

空判斷顯示

android:text="@{user.displayName ?? user.lastName}"
android:text="@{user.displayName != null ? user.displayName : user.lastName}"

 

android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"

 Includes 使用

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <include layout="@layout/name"//佈局name.xml
           bind:user="@{user}"/>
       <include layout="@layout/contact"//佈局contact.xml
           bind:user="@{user}"/>
   </LinearLayout>
</layout>

include界面 中 還是user 定義,纔可進行數據綁定使用

  <data>
       <variable name="user" type="com.example.User"/>
   </data>

背景需要 Drawable 對象,但顏色是一個整數,不管何時有 Drawable 並且返回值是一個整數,那麼整數類型會被轉換爲ColorDrawable 看,這個轉換是通過使用帶帶有 BindingConversion 註解的靜態方法完成的:

@BindingConversion
public static ColorDrawable convertColorToDrawable(int color) {
   return new ColorDrawable(color);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章