DataBinding基礎使用三

稀飯_發表於2018-08-10

一繫結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");
        });
    }
}複製程式碼





相關文章