Jetpack 使用

一 VM使用

  1. 创建类A继承ViewModel
  2. 在A中写入基础属性
  3. 在Activity中创建A
MainVM mainVM=new  ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MainVM.class);

二 LiveData+ViewModel

  1. 创建vm 继承 ViewModel
  2. 在vm中添加livedata属性
private MutableLiveData<Integer> count;
  1. 添加count的get方法
public MutableLiveData<Integer> getCount() {
        if (count==null){
            count=new MutableLiveData<>();
            count.setValue(0);
        }
        return count;
    }
  1. 在Activity中创建VM
mainVM = new  ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MainVM.class);
  1. 添加监听
mainVM.getCount().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {

                tv.setText(String.valueOf(integer));
            }
        });

三 databinding

1 简单使用

  1. 添加依赖
  //启用DataBinding
    buildFeatures {
        dataBinding true
    }
  1. 布局文件修改为支持数据绑定 ,第一行option+enter
  2. 编写Model
public class User  {
    public    String name;
    public    int lever;

    public User(String account, int pwd) {
        this.name = account;
        this.lever = pwd;
    }


}
  1. 将Model放入布局中文件中
    <data>
            <variable
                name="user"
                type="com.example.myapplication.User" />
    </data>
  1. 在布局文件中使用
<TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="48dp"
            
            android:text="@{user.name}"
            
            app:layout_constraintTop_toTopOf="@+id/guideline2" />

2 import 标签 和 事件绑定

A. import标签

  1. 创建类StarUtils
public class StarUtils {
    public static String start(int start){
        switch (start){
            case 1:
                return "1 星";
            case 2:
                return "2 星";
            case 3:
                return "3 星";
            case 4:
                return "4 星";
            case 5:
                return "5 星";
        }
        return "";
    }

}
  1. 布局文件中引入
    <data>
     
        <import type="com.example.myapplication.StarUtils"/>
        
    </data>

  1. 使用@{类名.方法名(方法参数)}
   <TextView
            android:id="@+id/tv_level"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            
            android:text="@{StarUtils.start(user.lever)}"

B 事件绑定

  1. 创建类EventHandleListen
public class EventHandleListen {
    private Context context;

    public EventHandleListen(Context context) {
        this.context = context;
    }

    public  void  buttonClick(View view){
        Toast.makeText(context,"wocao",Toast.LENGTH_SHORT).show();
    }

}
  1. 在布局文件中引入
   <data>
        <variable
            name="eventHandle"
            type="com.example.myapplication.EventHandleListen" />
   </data>
  1. 在布局文件中使用@{类的替代名.方法}
  <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="44dp"
            android:text="Button"
            
            android:onClick="@{eventHandle.buttonClick}"
            
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent" />

3 二级页面传递

  1. 创建二级页面sub.xml
  2. 在一级页面中找到Containers->将sub.xml 添加进来
 <include
            layout="@layout/sub"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="36dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline2"
            app:user="@{user}" />
  1. 在二级页面中添加变量user,一级变量中也要存在
   <data>
        <variable
            name="user"
            type="com.example.myapplication.SUser" />
        <import type="com.example.myapplication.StarUtils"/>
    </data>
  1. 在一级页面中赋值给二级页面
 <include
            layout="@layout/sub"
        //左边的user代表二级页面中的变量,右边的代表一级页面的变量,赋值操作
            app:user="@{user}"
             />

3 双向绑定

  1. 写继承自BaseObservable的类SUser
public class SUser extends BaseObservable{
    public    String name;
    public    int lever;

    public SUser(String account, int pwd) {
        this.name = account;
        this.lever = pwd;
    }
    //当name属性发生变化时,由于调用了notify,所以这个@Bindable会自动被调用
    @Bindable
    public String getName() {
        return name;
    }
		
    public void setName(String name) {
        if ((name!=null) && (!name.equals(this.name))){
            this.name = name;
            notifyPropertyChanged(BR.name);
        }

    }
    @Bindable
    public int getLever() {
        return lever;
    }

    public void setLever(int lever) {
//        if ((lever!=null) && (lever!=this.lever))
        this.lever = lever;
                    notifyPropertyChanged(BR.lever);
    }
}
  1. 在xml文件中写入变量SUser
    <data>

      <variable
          name="user"
          type="com.example.myapplication.SUser" />
        <import type="com.example.myapplication.StarUtils"/>
    </data>

  1. 在xml文件中使用变量SUser
<EditText 
            android:text="@={user.name}"
             />

5 . 在mainActivity中使用

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        db = DataBindingUtil.setContentView(this, R.layout.activity_main);
        db.setVm(new UserViewModel());



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肉丸饭团

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值