赞
踩
视图绑定(ViewBinding)在开发中起到的作用是代替findViewById。
ViewBinding是按模块启动的,在使用之前需要在模块中的gradle中开启ViewBinding。
- //kotlin
- android {
- ...
- buildFeatures {
- viewBinding = true
- }
- }
-
- //groovy
- android {
- ...
- buildFeatures {
- viewBinding true
- }
- }
当我们为某个模块引入ViewBinding并Sync之后,系统会自动为该模块下的每一个xml生成一个绑定类,每个绑定类都包含该xml中的所有组件id的引用,而这些类的名称都与xml有关。例如我们已有一个布局文件名为result_profile.xml,他的绑定类名称就是ResultProfileBinding。
以下展示了如何在Activity和Fragment中使用生成的绑定类。
在onCreate中:
- //kotlin
- private lateinit var binding: ResultProfileBinding
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- binding = ResultProfileBinding.inflate(layoutInflater)//1+2
- setContentView(view)//3
- }
-
- //java
- private ResultProfileBinding binding;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- binding = ResultProfileBinding.inflate(getLayoutInflater());//1
- View view = binding.getRoot();//2
- setContentView(view);//3
- }
此后可以使用该绑定类的实例来引用任何视图:
- //kotlin
- private ResultProfileBinding binding;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- binding = ResultProfileBinding.inflate(getLayoutInflater());
- View view = binding.getRoot();
- setContentView(view);
- }
-
- //java
- binding.name.setText(viewModel.getName());
- binding.button.setOnClickListener(new View.OnClickListener() {
- viewModel.userClicked()
- });
Fragment和Activity中的用法差不多,需要在onCreateView方法中执行以下:
- //kotlin
- //该变量只在onCreateView和onDestroyView中可见
- private var _binding: ResultProfileBinding? = null
- private val binding get() = _binding!!
-
- override fun onCreateView(
- inflater: LayoutInflater,
- container: viewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- _binding = ResultProfileBinding.inflate(inflater, container, false)
- val view = binding.root
- return view
- }
-
- override fun onDestroyView() {
- super.onDetroy()
- _binding = null
- }
-
- //java
- private ResultProfileBinding binding;
-
- @Override
- public View onCreateView (LayoutInflater inflater,
- ViewGroup container,
- Bundle savedInstanceState) {
- binding = ResultProfileBinding.inflate(inflater, container, false);
- View view = binding.getRoot();
- return view;
- }
-
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- binding = null;
- }
由于kotlin中空类型系统的存在,在fragment中的ViewBinding写法与java相比会稍显复杂,需要在新建一个变量接收绑定的实例,并给出非空断言。
两者都可用于直接引用视图的绑定类,ViewBinding的优势在于:
而与findViewById即数据绑定相比,ViewBinding具有以下限制:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。