Android viewBinding让你告别findViewById和ButterKnife

       很久没有更新博客了,不是因为别的,就是懒。今天要分享的一个新技术,从此告别定义一大串的UI控件变量,再也不用写findViewById,也不需要依赖ButterKnife和写一堆@BindView().听说这一消息,不知道大家有没有觉得很兴奋,反正我是觉得很香。那么下面就开始介绍一下viewBind。

一、使用viewBinding

       1.首先在你想要使用viewBinding的模块下的build.gradle下进行如下配置(你得使用Android studio3.6以上):

//Android studio为3.6
android{
    ......

    viewBinding{
        enabled = true
    }
}

//Android studio为4.0
android {
    ......
    buildFeatures {
        viewBinding = true
    }
}

       2.在相应的activity类中进行初始化

public class MainActivity extends BaseActivity {
    //视图绑定生成类文件
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());
        binding.tvNetworkHint.setText("viewBinding真香");
    }
}

     3.某个activity不想用viewBinding(用过的都说好,还是用吧)

//在相应的布局下配置
tools:viewBindingIgnore="true"

二、viewBinding是如何实现视图绑定

       在相应模块的build\generated\data_binding_base_class_source_out\debug\out\com\kaiyuan\police\databinding目录下找到相应的类,这个类是自动生成的。源码如下:

// Generated by view binder compiler. Do not edit!

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewbinding.ViewBinding;
import com.kaiyuan.police.R;
import java.lang.NullPointerException;
import java.lang.Override;
import java.lang.String;

public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final ConstraintLayout rootView;

  @NonNull
  public final TextView tvHello;

  @NonNull
  public final TextView tvNetworkHint;

  private ActivityMainBinding(@NonNull ConstraintLayout rootView, @NonNull TextView tvHello,
      @NonNull TextView tvNetworkHint) {
    this.rootView = rootView;
    this.tvHello = tvHello;
    this.tvNetworkHint = tvNetworkHint;
  }

  @Override
  @NonNull
  public ConstraintLayout getRoot() {
    return rootView;
  }

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

  @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);
  }

  @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.
    String missingId;
    missingId: {
      TextView tvHello = rootView.findViewById(R.id.tv_hello);
      if (tvHello == null) {
        missingId = "tvHello";
        break missingId;
      }
      TextView tvNetworkHint = rootView.findViewById(R.id.tv_network_hint);
      if (tvNetworkHint == null) {
        missingId = "tvNetworkHint";
        break missingId;
      }
      return new ActivityMainBinding((ConstraintLayout) rootView, tvHello, tvNetworkHint);
    }
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

通过源码可以发现:调用ActivityMainBinding.inflate()时创建父级view,然后再调用bind方法进行子UI的初始化造作,其实就是调用findViewById方法,最后调用该类的构造函数。

 

       整个看下来很清晰,很简单。再也不用手动写findViewById和依赖ButterKnife了。如果觉得不错,就点个赞吧!谢谢!

 

下面是我个人公众号,欢迎关注

Android viewBinding让你告别findViewById和ButterKnife_第1张图片

你可能感兴趣的:(Android)