美文网首页Android
MVVM -- 2 databind 绑定事件和页面刷新使用

MVVM -- 2 databind 绑定事件和页面刷新使用

作者: 清汤白面 | 来源:发表于2020-10-09 11:22 被阅读0次

    1.xml里面

    <?xml version="1.0" encoding="utf-8"?>
    <layout
        xmlns:android="http://schemas.android.com/apk/res/android"
      >
        <data>
            <variable
                name="user"
                type="com.melo.app.mvvm.UserInfo" />
    
            <variable
                name="click"
                type="com.melo.app.mvvm.MainActivity" />
        </data>
    
        <LinearLayout
              ...>
             <TextView
                ...
                android:text="@{user.name}"
              />
             <TextView
                ...
                android:text="@{user.sex}"
              />
    
    
            <Button
               ...
                android:onClick="@{(v)->click.changeText(v)}"
                android:text="改变用户信息" />
        </LinearLayout>
    </layout>
    

    2.activity中

    public class MainActivity extends AppCompatActivity {
    
        UserInfo userInfo;
        ActivityMainBinding viewDataBinding;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
            userInfo = new UserInfo("德玛西亚",  "男"));
            viewDataBinding.setUser(userInfo);
            viewDataBinding.setClick(this);
        }
    
        public void changeText(View view) {
            Toast.makeText(this, userInfo.getName(), Toast.LENGTH_SHORT).show();
        }
    }
    
    public class UserInfo {
    
       String name;
       String sex;
    
        public UserInfo(String name, String sex) {
            this.name = name;
            this.sex = sex;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    }
    

    这样我们的点击事件 就搞定了。

    3.更改用户信息的数据 刷新界面

    我们就在按钮的点击事件中去更改用户信息

    1.方法(直接重新设置下user)

    int i = 0;
    
        public void changeText(View view) {
            userInfo.setName("德玛西亚" + i);
            i++;
            Toast.makeText(this, userInfo.getName(), Toast.LENGTH_SHORT).show();
            viewDataBinding.setUser(userInfo);
        }
    

    2.利用BaseObservable

    public class UserInfo extends BaseObservable {
    
        @Bindable
        String name;
    
        @Bindable
        String sex;
    
        public UserInfo(String name, String sex) {
            this.name = name;
            this.sex = sex;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
            notifyPropertyChanged(BR.sex);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
            notifyPropertyChanged(BR.name);
        }
    }
    
    public class MainActivity extends AppCompatActivity {
    
        UserInfo userInfo;
        ActivityMainBinding viewDataBinding;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
            userInfo = new UserInfo("德玛西亚","男");
            viewDataBinding.setUser(userInfo);
            viewDataBinding.setClick(this);
    
        }
    
        int i = 0;
    
        public void changeText(View view) {
            userInfo.setName("德玛西亚" + i);
            i++;
            Toast.makeText(this, userInfo.getName(), Toast.LENGTH_SHORT).show();
        }
    }
    

    3.利用ObservableField

    源码中可以看到ObservableField是继承BaseObservableField,就是对BaseObservableField个一个封装。归根到底就是用的观察者模式,监听数据是否有改变,然后刷新界面。

    用法如下:
    1.xml布局文件不变

    public class UserInfo { 
        
        ObservableField<String> name;
        ObservableField<String> sex;
    
        public UserInfo(ObservableField<String> name, ObservableField<String> sex) {
            this.name = name;
            this.sex = sex;
        }
    
        public ObservableField<String> getName() {
            return name;
        }
    
        public void setName(ObservableField<String> name) {
            this.name = name;
        }
    
        public ObservableField<String> getSex() {
            return sex;
        }
    
        public void setSex(ObservableField<String> sex) {
            this.sex = sex;
        }
    }
    
    public class MainActivity extends AppCompatActivity {
    
        UserInfo userInfo;
        ActivityMainBinding viewDataBinding;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
            userInfo = new UserInfo(new ObservableField<String>("德玛西亚"), new ObservableField<String>("男"));
            viewDataBinding.setUser(userInfo);
            viewDataBinding.setClick(this);
    
        }
    
        int i = 0;
    
        public void changeText(View view) {
            userInfo.getName().set("德玛西亚" + i);
            i++;
            Toast.makeText(this, userInfo.getName().get(), Toast.LENGTH_SHORT).show();
        }
    }
    

    运行

    原来这就是那来自北方的南蛮汉子 2020-10-10 11.15.08(1).gif

    关于Livedata 结合 databind 我放在后面再更新

    a

    相关文章

      网友评论

        本文标题:MVVM -- 2 databind 绑定事件和页面刷新使用

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