ViewBinding

官网

一、装备

build.gradle 文件装备

android {
    //...
    viewBinding {
        enabled = true
    }
	  //或许gradle plugin 7.0以上也能够用如下写法
		buildFeatures {
        viewBinding true
    }
}

如果希望在生成绑定类是疏忽某个布局文件,tools:viewBindingIgnore="true"增加特点到布局文件的根视图中

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
    xmlns:tools="<http://schemas.android.com/tools>"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    **tools:viewBindingIgnore="true**">
</androidx.constraintlayout.widget.ConstraintLayout>

二、运用

2.1、创立XML视图文件

当时模块启用视图绑定功用后,系统会为该模块中包括的每个XML生成一个绑定类。每个绑定类均包括对根视图已经具有ID 的一切视图的运用。

系统生成绑定类称号:将XML文件的姓名转换为驼峰式大小写,并在末尾增加Binding一词

假设布局文件名为:fragment_customize.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
    xmlns:app="<http://schemas.android.com/apk/res-auto>"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/firstView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
    <Button
        android:id="@+id/threeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

按照上面的规则说明,系统会给咱们生成FragmentCustomizeBinding 文件(文件方位在build/generated/data_binding_base_class_source_out目录下)。

// Generated by view binder compiler. Do not edit!
package xxx;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewbinding.ViewBinding;
import androidx.viewbinding.ViewBindings;
import com.mumu.joker.R;
import java.lang.NullPointerException;
import java.lang.Override;
import java.lang.String;
public final class FragmentCustomizeBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;
  @NonNull
  public final TextView firstView;
  @NonNull
  public final Button threeView;
  private FragmentCustomizeBinding(@NonNull ConstraintLayout rootView, @NonNull TextView firstView,
      @NonNull Button threeView) {
    this.rootView = rootView;
    this.firstView = firstView;
    this.threeView = threeView;
  }
  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }
  @NonNull
  public static FragmentCustomizeBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }
  @NonNull
  public static FragmentCustomizeBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup parent, boolean attachToParent) {
    View root = inflater.inflate(R.layout.fragment_customize, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }
  @NonNull
  public static FragmentCustomizeBinding 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.firstView;
      TextView firstView = ViewBindings.findChildViewById(rootView, id);
      if (firstView == null) {
        break missingId;
      }
      id = R.id.threeView;
      Button threeView = ViewBindings.findChildViewById(rootView, id);
      if (threeView == null) {
        break missingId;
      }
      return new FragmentCustomizeBinding((ConstraintLayout) rootView, firstView, threeView);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

所生成的绑定类详细三个特点:rootViewfirstViewthreeView 。能够看到没有ID的在绑定类中是不存在引证的。

rootView 特点是私用的,能够通过getRoot()办法获取根视图的引证

2.2、在Activity中运用

ActvityonCreate()办法中执行以下步骤:

  1. 调用绑定类的inflate()静态办法,创立绑定类的实例。
  2. 调用绑定类的getRoot()办法或许kotlin的特点语法获取根视图的引证
  3. 调用Activity的setContentView()办法,传递根视图目标
  4. 后续即可运用绑定类的实例来引证XML任何具有ID的视图
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
  }

2.3、在Fragment中运用

FragmentonCreateView()办法中执行以下步骤:

  1. 调用绑定类的inflate()静态办法,创立绑定类的实例。
  2. 调用绑定类的getRoot()办法或许kotlin的特点语法获取根视图的引证
  3. onCreateView()办法中回来根视图
  4. 后续即可运用绑定类的实例来引证XML任何具有ID的视图
private var _binding: FragmentCustomizeBinding? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
		_binding = FragmentCustomizeBinding.inflate(inflater, container, false)
    return binding.root
  }
override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
  }