JetPack 备忘录

/ 默认分类 / 没有评论 / 23浏览

Jetpack

Android Jetpack 是一套组件、工具和指导,可以帮助您构建出色的 Android 应用。

DataBinding

DataBinding开关

    dataBinding {
        enabled = true
    }

基本使用


<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="viewmodel"
            type="com.myapp.data.ViewModel" />
    </data>
    ……
    <TextView
      android:text="@{viewmodel.userName}" />
    
    <View
     android:background="@{isError ? @color/red : @color/white}"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>
    
    <CheckBox
     android:id="@+id/login_remember_pwd"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:checked="true"
     android:onCheckedChanged="@{(v, isChecked)->presenter.onRememberPwdStatusChanged(isChecked)}"
     />
    ……
</layout>

Binding adapters

@BindingAdapter({"imageUrl", "error"})
public static void loadImage(ImageView view, String url, Drawable error) {
  Picasso.get().load(url).error(error).into(view);
}

与RecyclerView配合使用

    public static class BindingViewHolder extends RecyclerView.ViewHolder {
        public ViewDataBinding mBinding;

        public BindingViewHolder(@NonNull ViewDataBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }

Lifecycles

Room

ViewModel