美文网首页
DataBinding部分踩坑

DataBinding部分踩坑

作者: 我有一口小白牙 | 来源:发表于2017-03-31 15:03 被阅读210次

    以下是databinding使用的一个小demo, 自己练习用的, 目前觉得不好用, 暂时就先放弃了, 但是记录在简书上, 记录一些自己踩过的坑
    添加依赖:
    android {
    .......
    //在这里设置是说此项目支持databinding
    dataBinding {
    enabled = true
    }
    }

    xml(看到简书这样的排版我也是醉了):
    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class=".Custom">
        <import type="java.util.HashMap"/>
        <import type="java.util.ArrayList"/>
        <import type="android.graphics.Bitmap"/>
        <import type="com.diandou.demo5_databinding.Student" alias="tudent"/>
    
        <import type="com.diandou.demo5_databinding.EventHandlers"></import>
    
        <variable
            name="handlers"
            type="EventHandlers"/>
        
        <variable
            name="stu"
            type="tudent" />
    
        <variable
            name="str"
            type="String"/>
        <variable
            name="error"
            type="boolean"/>
        <variable
            name="num"
            type="int"/>
        <variable
            name="map"
            type="HashMap<String, String>" />
    
        <variable
            name="list"
            type="ArrayList<String>" />
    
        <variable
            name="click"
            type="com.diandou.demo5_databinding.MainActivity"/>
    
        <variable
            name="people"
            type="com.diandou.demo5_databinding.People"/>
    </data>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{stu.name}" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{stu.addr}" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{str}" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(num)}" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="----------" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{list[num]}" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{map[str]}" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{error ? "ok" : "error"}' />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Click me"
            android:onClick="@{handlers.handleClick}"
            />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{click.click}"
            android:text="@{stu.name}"
            />
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{people.name}"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{String.valueOf(people.age)}"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text='@{people.isMan ? "man":"women"}'
                />
        </LinearLayout>
    </LinearLayout>
    

    </layout>

    实体类:

    import android.databinding.BaseObservable;
    import android.databinding.Bindable;

    /**

    • Created by baiya on 2017/3/30.
      */

    public class Student extends BaseObservable{

    private String name;
    private String addr;
    
    public Student() {
    }
    
    public Student(String name, String addr) {
        this.name = name;
        this.addr = addr;
    }
    
    @Bindable
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
    
    @Bindable
    public String getAddr() {
        return addr;
    }
    
    public void setAddr(String addr) {
        this.addr = addr;
        notifyPropertyChanged(BR.addr);
    }
    

    }

    另一个bean类
    import android.databinding.ObservableBoolean;
    import android.databinding.ObservableField;
    import android.databinding.ObservableInt;

    /**

    • Created by baiya on 2017/3/31.
      */

    public class People {
    public ObservableField<String> name = new ObservableField<>();
    public ObservableInt age = new ObservableInt();
    public ObservableBoolean isMan = new ObservableBoolean();
    }

    相应点击事件的类
    package com.diandou.demo5_databinding;

    import android.view.View;
    import android.widget.Toast;

    /**

    • Created by baiya on 2017/3/31.
      */

    public class EventHandlers {
    public void handleClick(View view) {
    Toast.makeText(view.getContext(), "you clicked the view", Toast.LENGTH_LONG).show();
    }
    }

    main:

    import android.databinding.DataBindingUtil;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;

    import com.diandou.demo5_databinding.Custom;

    import java.util.ArrayList;
    import java.util.HashMap;

    public class MainActivity extends AppCompatActivity {

    private Student student;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Custom binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        student = new Student("loader", "山东");
        binding.setStu(student);
        binding.setStr("lallalla");
        binding.setNum(1);
        binding.setError(true);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("lal");
        strings.add("lal---");
        binding.setStr("lallalla");
        binding.setList(strings);
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("lallalla","909090");
        binding.setMap(stringStringHashMap);
        binding.setHandlers(new EventHandlers());
        binding.setClick(this);
        People mPeople = new People();
        binding.setPeople(mPeople);
        mPeople.name.set("people");
        mPeople.age.set(19);
        mPeople.isMan.set(true);
    
    }
    
    public void click(View view) {
        student.setName("qin");
    }
    

    }

    相关文章

      网友评论

          本文标题:DataBinding部分踩坑

          本文链接:https://www.haomeiwen.com/subject/sjjxgttx.html