DataBanding

Jetpack学习(二)——databinding(2)

2020-01-09  本文已影响0人  岁月静好浅笑安然

databinding(2)

1.绑定数据其他类型

实例代码activity_test.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">

    <data>
        <import type="android.util.SparseArray"/>
        <import type="java.util.Map"/>
        <import type="java.util.List"/>
        <variable name="list" type="List&lt;String>"/>
        <!--<variable name="sparse" type="String []"/>-->
        <variable name="map" type="Map&lt;String, String>"/>
        <variable name="index" type="int"/>
        <variable name="key" type="String"/>
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.TestActivity">

               <TextView
                   android:text="@{key}"
                   android:layout_margin="10dp"
                   android:textSize="20sp"
                   tools:text="测试str"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" />

               <TextView
                   android:textColor="@{index>3?@color/star:@color/colorAccent}"
                   android:text="@{String.valueOf(index)}"
                   android:layout_margin="10dp"
                   android:textSize="20sp"
                   tools:text="测试int"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" />

               <TextView
                   android:text="@{list.get(0)}"
                   android:layout_margin="10dp"
                   android:textSize="20sp"
                   tools:text="测试List"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" />

        

              <TextView
                   android:text="@{map[`age`]}"
                   android:layout_margin="10dp"
                   android:textSize="20sp"
                   tools:text="测试Map"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" />
    </LinearLayout>
</layout>

TestActivity.java

public class TestActivity extends AppCompatActivity {
    private List<String> _list;
    private Map<String, String> _map;
    private String[] arr={"数组一","数组二"};
    private String str="我是key";
    private int index=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        ActivityTestBinding testBinding=DataBindingUtil.setContentView(this,R.layout.activity_test);
        testBinding.setKey(str);
        testBinding.setIndex(index);
        testBinding.setList(_list);
        testBinding.setMap(_map);
        //testBinding.setSparse(arr);
    }

    private void initData() {
        _list=new ArrayList<>();
        _map=new HashMap<>();
        _map.put("age","88");
        _list.add("集合一");
        _list.add("集合二");
    }
}

device-2020-01-09-153735.png

2.还支持表达式语言

常见功能

表达式语言与托管代码中的表达式非常相似。您可以在表达式语言中使用以下运算符和关键字:

(请注意,< 需要转义为 &lt;

实例代码

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

缺少的运算

您可以在托管代码中使用的表达式语法中缺少以下运算:

某些资源需要显式类型求值,如下表所示:此处指的是引用res/values中的

类型 常规引用 表达式引用
String[] @array @stringArray
int[] @array @intArray
TypedArray @array @typedArray
Animator @animator @animator
StateListAnimator @animator @stateListAnimator
color int @color @color
ColorStateList @color @colorStateList

实例代码

       android:textColor="@{index>3?@color/star:@color/colorAccent}"

事件处理

3.事件绑定

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
     
        <variable
            name="ClickListener"
            type="android.view.View.OnClickListener" />
       
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.TestActivity">
            
               <TextView
                   android:id="@+id/textView1"
                   android:onClick="@{ClickListener}"
                   android:text="点我1"
                   android:layout_margin="10dp"
                   android:textSize="20sp"
                   tools:text="测试List"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" />

              <TextView
                   android:id="@+id/textView2"
                   android:onClick="@{ClickListener}"
                   android:text="点我二"
                   android:layout_margin="10dp"
                   android:textSize="20sp"
                   tools:text="测试Map"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" />
    </LinearLayout>
</layout>
        ...
        testBinding.setClickListener(this);
        ...
        //处理点击事件
        @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.textView1:
                Toast.makeText(TestActivity.this,"点击了11",Toast.LENGTH_SHORT).show();
                break;
            case R.id.textView2:
                Toast.makeText(TestActivity.this,"点击了22",Toast.LENGTH_SHORT).show();
                break;
        }

    }
        
        

方法引用

    public class MyHandlers {
        public void onClickFriend(View view) { ... }
    }

        

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
       <data>
           <variable name="handlers" type="com.example.MyHandlers"/>
           <variable name="user" type="com.example.User"/>
       </data>
       <LinearLayout
           android:orientation="vertical"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
           <TextView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@{user.firstName}"
               android:onClick="@{handlers::onClickFriend}"/>
       </LinearLayout>
    </layout>
       testBinding.setHandlers(new MyHandlers());

4. 导入、变量和包含

数据绑定库提供了诸如导入、变量和包含等功能。通过导入功能,您可以轻松地在布局文件中引用类。通过变量功能,您可以描述可在绑定表达式中使用的属性。通过包含功能,您可以在整个应用中重复使用复杂的布局。

导入

通过导入功能,您可以轻松地在布局文件中引用类,就像在托管代码中一样。您可以在 data 元素使用多个 import 元素,也可以不使用。以下代码示例将 View 类导入到布局文件中:

<data>
        <import type="android.view.View"/>
    </data>

导入 View 类可让您通过绑定表达式引用该类。以下示例展示了如何引用 View 类的 VISIBLE 和 GONE 常量:

<TextView
       android:text="@{user.lastName}"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>

类型别名
当类名有冲突时,包名不同类名相同就会冲突其中一个类可使用别名重命名。以下示例将 com.example.real.estate 软件包中的 View 类重命名为 Vista:

<import type="android.view.View"/>
    <import type="com.example.real.estate.View"
            alias="Vista"/>

您可以在布局文件中使用 Vista 引用com.example.real.estate.View,使用 View 引用android.view.View

导入其他类
导入的类型可用作变量和表达式中的类型引用。以下示例显示了用作变量类型的 User 和 List:

<data>
        <import type="com.example.User"/>
        <import type="java.util.List"/>
        <variable name="user" type="User"/>
        <variable name="userList" type="List&lt;User>"/>
    </data>

注意:Android Studio 尚不处理导入,因此导入变量的自动填充功能可能无法在您的 IDE 中使用。您的应用仍可以编译,并且您可以通过在变量定义中使用完全限定名称来解决这个 IDE 问题。
您还可以使用导入的类型来对表达式的一部分进行类型转换。以下示例将 connection 属性强制转换为类型 User:

<TextView
       android:text="@{((User)(user.connection)).lastName}"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

在表达式中引用静态字段和方法时,也可以使用导入的类型。以下代码会导入 MyStringUtils 类,并引用其 capitalize 方法:

<data>
        <import type="com.example.MyStringUtils"/>
        <variable name="user" type="com.example.User"/>
    </data>
    …
    <TextView
       android:text="@{MyStringUtils.capitalize(user.lastName)}"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
    

就像在托管代码中一样,系统会自动导入 java.lang.*。

变量

您可以在 data 元素中使用多个 variable 元素。每个 variable 元素都描述了一个可以在布局上设置、并将在布局文件中的绑定表达式中使用的属性。以下示例声明了 user、image 和 note 变量:

<data>
        <import type="android.graphics.drawable.Drawable"/>
        <variable name="user" type="com.example.User"/>
        <variable name="image" type="Drawable"/>
        <variable name="note" type="String"/>
    </data>

变量类型在编译时进行检查,因此,如果变量实现 Observable 或者是可观察集合,则应反映在类型中。如果该变量是不实现 Observable 接口的基类或接口,则变量是“不可观察的”。
如果不同配置(例如横向或纵向)有不同的布局文件,则变量会合并在一起。这些布局文件之间不得存在有冲突的变量定义。
在生成的绑定类中,每个描述的变量都有一个对应的 setter 和 getter。在调用 setter 之前,这些变量一直采用默认的托管代码值,例如引用类型采用 null,int 采用 0,boolean 采用 false,等等。
系统会根据需要生成名为 context 的特殊变量,用于绑定表达式。context 的值是根视图的 getContext() 方法中的 Context 对象。context 变量会被具有该名称的显式变量声明替换。

包含

通过使用应用命名空间和特性中的变量名称,变量可以从包含的布局传递到被包含布局的绑定。以下示例展示了来自 name.xml 和 contact.xml 布局文件的被包含 user 变量:

<?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"
               bind:user="@{user}"/>
           <include layout="@layout/contact"
               bind:user="@{user}"/>
       </LinearLayout>
    </layout>
    

数据绑定不支持 include 作为 merge 元素的直接子元素。例如,以下布局不受支持:

<?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>
       <merge><!-- Doesn't work -->
           <include layout="@layout/name"
               bind:user="@{user}"/>
           <include layout="@layout/contact"
               bind:user="@{user}"/>
       </merge>
    </layout>

错误: 找不到符号
符号: 类 ActivityTestBindingImpl
位置: 程序包 com.hwp.databindingdemo.databinding
当新手使用databinding出现这个错误的时候,一般来说就是xml文件中与databinding相关的写错

上一篇 下一篇

猜你喜欢

热点阅读