ViewBinding使用及原理

一、什么是ViewBinding

View Binding是Android Studio 3.6推出的新特性,旨在替代findViewById(内部实现还是使用findViewById)。通过ViewBinding,可以更轻松地编写可与视图交互的代码。在模块中启用ViewBinding之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。

在大多数情况下,视图绑定会替代 findViewById

注意:视图绑定在 Android Studio 3.6 Canary 11 及更高版本中可用

二、ViewBinding怎么使用

启用Viewbinding
在模块build.gradle文件android节点下添加如下代码

android {
      
    viewBinding{
        enabled = true
    }
}

在 Android Studio 4.0 中,viewBinding 变成属性被整合到了 buildFeatures 选项中,所以配置要改成:

// Android Studio 4.0
android {
 buildFeatures {
  viewBinding = true
 }
}

重新编译后系统会为每个布局文件生成对应的Binding类,该类中包含对应布局中具有 ID 的所有视图的直接引用。生成类的目录在 模块根目录/build/generated/data_binding_base_class_source_out下。

在Activity中使用
// activity_main.xml




    


ActivityMainBinding mViewBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mViewBinding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(mViewBinding.getRoot());
    ...
}
kotlin:
package androidstack.viewbinding

import android.os.Bundle
import androidstack.viewbinding.databinding.ActivityMainBinding
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //获取绑定类实例
        binding = ActivityMainBinding.inflate(layoutInflater)
        //通过绑定类获取布局后,传给setContentView设置内容视图
        setContentView(binding.root)
        //通过绑定类获取对应的ID视图,进行操作
        binding.tvHelloWorld.text = "视图绑定"
    }
}

Binding文件名和布局中控件根据驼峰命名规则生成;
使用的时候在Activity的onCreate方法里调用其静态inflate方法,返回ViewBinding实例,通过ViewBinding实例可以直接访问布局文件中带id的控件,比如上面的TextView, mViewBinding.tvTextView
如果想在生成绑定类时忽略某个布局文件,将 tools:viewBindingIgnore="true"属性添加到相应布局文件的根视图中。

使用View Binding 写的基类
abstract class BaseActivity : AppCompatActivity() {
    private lateinit var _binding: T
    protected val binding get() = _binding;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = getViewBinding()
        setContentView(_binding.root)
    }

    protected abstract fun getViewBinding(): T
}

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.textView.text = "这是MainActivity"
    }

    override fun getViewBinding() = ActivityMainBinding.inflate(layoutInflater)
}
abstract class BaseFragment : Fragment() {
    private lateinit var _binding: T
    protected val binding get() = _binding;
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = getViewBinding(inflater, container)
        return _binding.root
    }
    
    protected abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): T
}

class FirstFragment : BaseFragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.textView.text = "这是FirstFragment"
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = FragmentFirstBinding.inflate(inflater, container, false)
}
Fragment中使用
public class MyFragment extends Fragment {
  private FragmentMyBinding binding;

  public MyFragment() {

  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      binding = FragmentMyBinding.inflate(inflater, container, false);
      return binding.getRoot();
  }

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      binding.textView.setText("这是Fragment");
      binding.button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Log.d("Fragment", "点击了按钮");
          }
      });
  }

  @Override
  public void onDestroy() {
      super.onDestroy();
      binding = null;
  }
在Dialog中的使用
class MyDialog extends Dialog {
    protected View mView;
    protected MyDialogBinding mBinding;

    public MyDialog(@NonNull Context context) {
        super(context,R.style.Dialog);
        mBinding = MyDialogBinding.inflate(getLayoutInflater());
        mView = mBinding.getRoot();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        Display d = window.getWindowManager().getDefaultDisplay();
        lp.width = (int) (d.getWidth() * 0.9F);
        window.setAttributes(lp);
    }
  

}
在 Adapter 中的使用
public class MainAdapter extends RecyclerView.Adapter {

    private List mList;

    public MainAdapter(List list) {
        mList = list;
    }

    @NonNull
    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //之前的写法
        //View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_comment, parent, false);
        //ViewHolder holder = new ViewHolder(view);

        //使用ViewBinding的写法
        LayoutCommentBinding commentBinding = LayoutCommentBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        ViewHolder holder = new ViewHolder(commentBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int position) {
        holder.mTextView.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTextView;

        //之前的写法
        //public ViewHolder(@NonNull View itemView) {
        //    super(itemView);
        //    mTextView = itemView.findViewById(R.id.tv_include);
        //}

        //使用ViewBinding的写法
        ViewHolder(@NonNull LayoutCommentBinding commentBinding) {
            super(commentBinding.getRoot());
            mTextView = commentBinding.tvInclude;
        }

    }
}


自定义View中使用

如果我们的自定义View中使用了layout布局,比如layout_my_view.xml,如下




    


会生成一个名为LayoutMyViewViewBinding.java文件,在自定义View通过如下方式绑定,

public class MyView extends View {
    public MyView (Context context) {
        this(context, null);
    }

    public MyView (Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutMyViewViewBinding viewBinding = LayoutMyViewViewBinding.inflate(LayoutInflater.from(getContext()), this, true);
    }
}

如果自定义View布局文件中使用merge标签,




    


此时要写成下面这个样子,

LayoutMyViewViewBinding viewBinding = LayoutMyViewViewBinding.inflate(LayoutInflater.from(context), this);
include 标签的使用

include 标签不带 merge 标签,需要给 include 标签添加 id, 直接使用 id 即可,用法如下所示。



ActivityMainBinding binding = ActivityMainBinding.inflate(layoutInflater);
binding.include.includeTvTitle.setText("使用 include 布局中的控件, 不包含 merge");

include 标签带 merge 标签,需要通过bind()将merge布局绑定到主布局上,用法如下所示。



ActivityMainBinding binding = ActivityMainBinding.inflate(layoutInflater);
LayoutMergeItemBinding mergeItemBinding = LayoutMergeItemBinding.bind(binding.getRoot());
mergeItemBinding.mergeTvTitle.setText("使用 include 布局中的控件, 包含 merge");

三、原理

原理就是Google在那个用来编译的gradle插件中增加了新功能,当某个module开启ViewBinding功能后,编译的时候就去扫描此模块下的layout文件,生成对应的binding类。

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

    @NonNull
    public final TextView tvTextView;

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

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

可以看出,最终使用的仍然是findViewById,和ButterKnife异曲同工,不同的是ButterKnife通过编译时注解生成ViewBinding类,而ViewBinding是通过编译时扫描layout文件生成ViewBinding类。

四、与findViewById相比优点

与使用 findViewById 相比,视图绑定具有一些很显著的优点:

  • Null 安全:由于视图绑定会创建对视图的直接引用,因此不存在因视图 ID 无效而引发 Null 指针异常的风险。此外,如果视图仅出现在布局的某些配置中(比如横竖屏布局内容差异),则绑定类中包含其引用的字段会使用 @Nullable 标记。
  • 类型安全:每个绑定类中的字段均具有与它们在 XML 文件中引用的视图相匹配的类型。这意味着不存在发生类转换异常的风险。
    这些差异意味着布局和代码之间的不兼容将会导致构建在编译时(而非运行时)失败。

你可能感兴趣的:(ViewBinding使用及原理)