DataBinding基础使用三

985 阅读3分钟

一绑定List/Map等集合数据

<?xml version="1.0" encoding="utf-8"?><!--布局以layout作为根布局-->
<layout>

    <data>
        <import type="java.util.ArrayList" />
        <import type="java.lang.String" />

        <variable
            name="list"
            type="ArrayList&lt;String>" />

        <import type="java.util.Map" />
        <variable
            name="map"
            type="Map&lt;String,String&gt;" />

        <variable
            name="arrays"
            type="String[]" />
    </data>
    <!--我们需要展示的布局-->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{list[0]}" />
        <!--List集合既可以和数组一样通过索引获取值list[index]方式,也可以调用API-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{list.get(1)}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{map[`name`]}" />
        <!--Map集合既可以通过map[key]的方式,也可以通过调用API-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{map.get(`age`)}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{arrays[0]}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{arrays[1]}" />

    </LinearLayout>
</layout>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        ArrayList<String> list = new ArrayList<>();
        list.add("first");
        list.add("second");
        binding.setList(list);

        Map<String, String> map = new HashMap<>();
        map.put("name", "zhangsan");
        map.put("age", "40");
        binding.setMap(map);

        String[] arrays = {"lisi", "laowang"};
        binding.setArrays(arrays);

    }
}

二Observable数据改变自动更新

Observable是一个接口,它的子类BaseObservable,ObservableField,ObservableBoolean, ObservableByte, ObservableChar, ObservableShort, ObservableInt, ObservableLong, ObservableFloat,ObservableDouble,ObservableParcelableObservableArrayList,ObservableArrayMap

<?xml version="1.0" encoding="utf-8"?><!--布局以layout作为根布局-->
<layout>

    <data>
        <variable
            name="animal"
            type="com.example.administrator.testdatabinding.Animal"/>

        <variable
            name="list"
            type="android.databinding.ObservableArrayList&lt;String&gt;"/>

        <variable
            name="map"
            type="android.databinding.ObservableArrayMap&lt;String,String&gt;"/>
    </data>
    <!--我们需要展示的布局-->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{animal.name}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{String.valueOf(animal.age)}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{list[0]}" />
        <!--Map集合既可以通过map[key]的方式,也可以通过调用API-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{list[1]}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{map[`name`]}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@{map[`age`]}" />

        <Button
            android:id="@+id/four_btn"
            android:layout_width="match_parent"
            android:text="改变数据"
            android:layout_height="wrap_content" />

    </LinearLayout>
</layout>
public class Animal {

    public final ObservableField<String> name = new ObservableField<>();

    public final ObservableInt age = new ObservableInt();
}

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        final Animal animal = new Animal();

        animal.name.set("cat");
        animal.age.set(2);
        binding.setAnimal(animal);

        final ObservableArrayList<String> list = new ObservableArrayList<>();
        list.add("dog");
        list.add("mouse");
        binding.setList(list);

        final ObservableArrayMap<String, String> map = new ObservableArrayMap<>();
        map.put("name","Tom");
        map.put("age","4");
        binding.setMap(map);

        binding.fourBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animal.name.set("dog");
                animal.age.set(4);
                list.set(0,"cat");
                list.set(1,"dog");
                map.put("name","Sam");
                map.put("age","5");
            }
        });



    }
}
当Animal属性数据改变,list/map集合数据改变,会自动更新数据。

三Databinding与include标签的结合

<?xml version="1.0" encoding="utf-8"?><!--布局以layout作为根布局-->
<layout>

    <data >
        <variable
            name="con"
            type="com.example.administrator.testdatabinding.Content"/>
    </data>
    <!--我们需要展示的布局-->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            android:layout_height="56dp"
            android:layout_width="match_parent"
            bind:content="@{con}" />
        <!--通过命名空间将写有toolbar的xml文件中定义的content对象作为属性绑定con对象,这2个对象是同一个类-->
        <TextView
            android:id="@+id/btn1"
            android:text="@string/app_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<layout >
    <data>

        <variable
            name="content"
            type="com.example.administrator.testdatabinding.Content"/>
    </data>

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="56dp"
        android:layout_width="match_parent"
        app:title="@{content.title}"
        app:subtitle="@{content.subTitle}"
        android:background="@color/colorPrimary"
        app:titleTextColor="@android:color/white"
        app:subtitleTextColor="@android:color/white" />
</layout>
public class Content extends BaseObservable {

    private String title;
    private String subTitle;

    public Content(String title, String subTitle) {
        this.title = title;
        this.subTitle = subTitle;
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        notifyPropertyChanged(BR.title);
    }

    @Bindable
    public String getSubTitle() {
        return subTitle;
    }

    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
        notifyPropertyChanged(BR.subTitle);
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        Content con = new Content("Title", "SubTitle");
        binding.setCon(con);
        //注意:这个测试没有效果,不会显示toolbar的title/subTitle
        //binding.toolbar.setContent(con);


        binding.btn1.setOnClickListener(view -> {
            binding.toolbar.toolbar.setTitle("EEEEEE");
            binding.toolbar.toolbar.setSubtitle("eeeeee");
        });
    }
}