浅析viewBinding和DataBinding

简介: viewBinding优点配置使用源码解析DataBinding配置创建实体类创建布局创建viewModeldataBinding绑定


浅析viewBinding和DataBinding



viewBinding


DataBinding


viewBinding



优点

当一个页面布局出现多个控件时,使用findViewById去进行控件绑定,过于冗长,且存在NULL指针异常风险。viewBinding直接创建对视图的引用,不存在因控件ID不存在而引发的NULL指针异常。并且在绑定类中对控件添加

findViewById viewBinding
冗长 简短
NULL NULL安全


配置

3.6之前的版本在build.gradle文件中声明如下定义

 viewBinding {
            enabled = true
        }

4.0以上的版本在build.gradle文件中声明如下定义

buildFeatures {
            viewBinding = true
        }

声明如上定义之后,点击同步(Sync Now)按钮,系统会自动生成viewBinding类,例如MainActivity会生成名为ActivityMainBinding的类,ReceiveActivity会生成名为ActivityReceiveBinding的类,以此类推;

以上viewBinding类会生成在如下路径文件中

build//generated//data_binding_base_class_source_out//debug//out//com.你的包名//databinding

image.png


使用

使用步骤很简单,需要被调用的控件声明id就行,然后声明viewBinding类对象

private ActivityMainBinding binding;

绑定视图

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

控件引用

binding.postMes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,ReceiverActivity.class));
            }
        });


源码解析

如上所示,我们使用了ActivityMainBinding.inflate()方法进行视图绑定和binding.getRoot()方法获取视图。

首先我们在外部通过调用ActivityMainBinding.inflate()方法。

@NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

然后内部进行重载,添加我们的Avcivity的布局文件,并调研bind(root)方法

@NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

在bind方法中进行控件绑定,通过其findChildViewById()方法

@NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.content;
      TextView content = ViewBindings.findChildViewById(rootView, id);
      if (content == null) {
        break missingId;
      }
      id = R.id.postMes;
      Button postMes = ViewBindings.findChildViewById(rootView, id);
      if (postMes == null) {
        break missingId;
      }
      return new ActivityMainBinding((LinearLayout) rootView, content, postMes);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }

然后在findChildViewById()方法中最终也使用到了findViewById()方法,但差距在于跳过视图本身

/**
     * Like `findViewById` but skips the view itself.
     *
     * @hide
     */
    @Nullable
    public static <T extends View> T findChildViewById(View rootView, @IdRes int id) {
        if (!(rootView instanceof ViewGroup)) {
            return null;
        }
        final ViewGroup rootViewGroup = (ViewGroup) rootView;
        final int childCount = rootViewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final T view = rootViewGroup.getChildAt(i).findViewById(id);
            if (view != null) {
                return view;
            }
        }
        return null;
    }

最后通过将获取到的控件定义与内部定义的字段进行缝合,以暴露给外部使用

 @NonNull
  private final LinearLayout rootView;
  @NonNull
  public final TextView content;
  @NonNull
  public final Button postMes;


private ActivityMainBinding(@NonNull LinearLayout rootView, @NonNull TextView content,
      @NonNull Button postMes) {
    this.rootView = rootView;
    this.content = content;
    this.postMes = postMes;
  }
dataBinding {
            enabled = true
        }


DataBinding


配置

依旧在build.gradle文件中配置如下定义

 
         

创建实体类

实体类通过继承BaseObservable类,而BaseObservable又实现了Observable,从而获取添加和移除监听的机制。

在get()方法中使用@Bindable注解,会自动生成BR类,此类中将添加@Bindable的字段声明成常量,然后在set()方法使用notifyPropertyChanged()配合使用,当数据发生变化时,dataBinding会自动修改该字段的值。

public class EventMessage extends BaseObservable {
    public String title;
    public EventMessage(){
    }
    public EventMessage(String title){
        this.title = title;
    }
    @Bindable
    public String getTitle() {
        return title;
    }
    /**
     * @param title*/
    public void setTitle(String title) {
        this.title = title;
        notifyPropertyChanged(BR.title);
    }
}


创建布局

创建layout标签布局才会生成ActivityMainBinding(以及布局文件名而定)

EditText通过使用如下定义进行绑定,

android:text="@={viewModel.message.title}"

Button通过如下定义进行点击事件监听

 android:onClick="@{viewModel.setText}"

以上两者的存在差距,EditText多了一个=,而Button没有,并且Button绑定监听事件,不需要加()

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="viewModel"
            type="com.franzliszt.databinding.ViewModel" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:gravity="center">
        <EditText
            android:id="@+id/inputText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="default"
            android:text="@={viewModel.message.title}"/>
        <TextView
            android:id="@+id/ShowText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="update"
            android:onClick="@{viewModel.setText}"/>
    </LinearLayout>
</layout>


创建viewModel

通过监听Button点击事件,在其中监听EditText输入事件,并将其输入的字符串显示在TextView中

public class ViewModel {
    private ActivityMainBinding binding;
    public EventMessage message;
    public ViewModel(ActivityMainBinding binding, EventMessage message){
        this.binding = binding;
        this.message = message;
    }
    public void setText(View view){
        String str = message.getTitle();
        binding.ShowText.setText(str);
    }
}


dataBinding绑定

private ActivityMainBinding binding;
binding = DataBindingUtil.setContentView( this,R.layout.activity_main );
binding.setViewModel(new ViewModel(binding,new EventMessage()));
相关文章
|
2月前
|
缓存 前端开发 开发工具
把 IntelliJ IDEA 的 Git 界面搬进 VSCode:GitCharm 使用说明
GitCharm 是 VSCode 插件,将 IntelliJ IDEA 的三栏式 Git 面板(分支树+提交图谱+文件详情)原生移植,支持多选压缩/精选、智能 reflog 救援、跨仓库管理及零延迟状态同步,兼顾高效与直觉。(239字)
221 0
|
编译器 Shell Android开发
工具技能学习(一):前置技能-makfile、make、.mk
工具技能学习(一):前置技能-makfile、make、.mk
1215 0
|
6月前
|
人工智能 Rust 并行计算
UV vs pip vs Conda:Python环境管理应该怎么选
Python包管理已进化:pip是标准但慢,conda专精数据科学与跨语言依赖,而uv——Rust打造的极速新锐,10~100倍提速,一站式替代pip+venv+pyenv+pip-tools,AI/大模型开发首选。
652 0
UV vs pip vs Conda:Python环境管理应该怎么选
|
缓存 调度 数据库
RxJava基础操作符和高级操作符
RxJava基于观察者模式,通过Observable、Observer、Subscription和Disposable四大核心实现响应式编程。支持冷热数据流,提供丰富操作符进行变换、过滤与组合,并借助Scheduler实现线程调度,Flowable解决背压问题,Subject用于状态共享,适用于异步事件处理与复杂数据流管理。
445 0
|
Android开发
08. 【Android教程】相对布局 RelativeLayout
08. 【Android教程】相对布局 RelativeLayout
797 0
|
网络安全 图形学 Android开发
Unity与安卓丨AS报错:SSL peer shut down incorrectly
Unity与安卓丨AS报错:SSL peer shut down incorrectly
Unity与安卓丨AS报错:SSL peer shut down incorrectly
|
缓存 JavaScript API
Flutter&鸿蒙next 状态管理框架对比分析
在 Flutter 开发中,状态管理至关重要,直接影响应用的性能和可维护性。本文对比分析了常见的状态管理框架,包括 setState()、InheritedWidget、Provider、Riverpod、Bloc 和 GetX,详细介绍了它们的优缺点及适用场景,并提供了 Provider 的示例代码。选择合适的状态管理框架需考虑应用复杂度、团队熟悉程度和性能要求。
1238 0
|
Android开发 C++
【Android NDK 开发】在 C 代码中获取 Android 系统信息 ( NDK 项目创建 | NDK 配置 | 获取 Android 系统版本号 )(二)
【Android NDK 开发】在 C 代码中获取 Android 系统信息 ( NDK 项目创建 | NDK 配置 | 获取 Android 系统版本号 )(二)
2456 0
【Android NDK 开发】在 C 代码中获取 Android 系统信息 ( NDK 项目创建 | NDK 配置 | 获取 Android 系统版本号 )(二)
|
存储 安全 物联网
Android经典实战之跳转到系统设置页面或其他系统应用页面大全
本文首发于公众号“AntDream”,关注获取更多技巧。文章总结了Android开发中跳转至系统设置页面的方法,包括设备信息、Wi-Fi、显示与声音设置等,并涉及应用详情与电池优化页面。通过简单的Intent动作即可实现,需注意权限与版本兼容性。每日进步,尽在“AntDream”。
3095 2
Gradle进阶篇(七)-AGP插件详解
前面几篇文章我们讲解了关于关于`Gradle的基础`,`Gradle生命周期`,`Gradle相关Api`的讲解,以及`Gradle自定义插件`,`Gradle Maven仓库管理`.今天这篇文章我们来讲解下`Android Gradle Plugin`相关知识。