Three lines of code to configure the ViewBinding

Paste the following code under Build. gradle android

android {

    
    viewBinding {
        enabled = true
    }
    
 }
Copy the code
Use ViewBinding in your Activity:

1 Import the generated Binging classes into the Activity. The following information is displayed:

// Com.mail. myApplication is the package name of the Activity. //AtyMainBinding is the binding for importing the XML layout. // For example, your layout file is named aty_main.xml The corresponding Binging classes are called AtyMainBinding // For example, the XML file named under your Layout file is called item_list. XML and the corresponding Binging classes are called ItemListBinding Import com.mail.myapplication.databinding.AtyMainBindingCopy the code

2 replaces setContentView

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // setContentView(R.layout.activity_main) var mBinding = AtyMainBinding.inflate(getLayoutInflater()); setContentView(mBinding.getRoot()); }}Copy the code

3 Obtain the control using Binding

Mbinding.mtvname. text = "binding.mtvname. text"Copy the code
Use ViewBinding in RecyclerView

Also very simple, calculate directly paste code

I’m going to write it inside here just to be lazy

inner class MyRecyclerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView. ViewHolder {the when (viewType) {0 - > {/ / ItemHomeListHeadBinding is corresponding to the layout file item_home_list_head. XML return fGoldViewHeadHolder(ItemHomeListHeadBinding.inflate(LayoutInflater.from(context))) } else -> { //ItemHomeListBinding Is the corresponding layout file item_home_list. XML return fGoldViewHolder (ItemHomeListBinding. Inflate (LayoutInflater. The from (context)))}}} override fun getItemCount(): Int = list.size override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {if (holder is fGoldViewHolder) {with(holder) {with(mBinding) {tvname. text = list[position] var url = "https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F0114%2Fea09bd73p00qmxkvg008wc000fk00f4m.png&thumb nail=650x2147483647&quality=80&type=jpg" ImageLoader.loadImage(activity, url, imgv) } } } if (holder is fGoldViewHeadHolder) { with(holder) { with(mBinding) { var adapter = ImageNetAdapter(list_img)  bannerX.adapter = adapter bannerX.addBannerLifecycleObserver(activity) bannerX.indicator = CircleIndicator(activity) bannerX.setOnBannerListener { data, position -> var map = data as HashMap<String, String> showToastS(position.toString() + "," + map.get("url")) } } } } } override fun getItemViewType(position: Int): Int { return position } inner class fGoldViewHolder(binding: ItemHomeListBinding) : RecyclerView.ViewHolder(binding.root) { var mBinding: ItemHomeListBinding = binding } inner class fGoldViewHeadHolder(binding: ItemHomeListHeadBinding) : RecyclerView.ViewHolder(binding.root) { var mBinding: ItemHomeListHeadBinding = binding } }Copy the code