当前位置:网站首页>Simple use of viewbinding

Simple use of viewbinding

2022-04-23 18:43:00 KIDD-4869

Preface

Kotlin Plug in 1.4.20 It is proposed to abandon , Google left us a year to modify and replace , What shall I do? ? I'm used to using it directly before xml Medium id As view Used , Do you want to go back to findViewById Era ? How is that possible? , Don't hurry to hug ViewBinding The arms of .
Google also knows Kotlin Disadvantages of plug-ins , such as :

  • adopt Kotlin Synthesis method (Synthetic View ) replace findViewById, This is through the global space cache ID, And Layout
    irrelevant , Not for ID Perform invalid check
  • In different Layout In file , Used the same ID, Or delete ID , It does not prompt null exceptions , It leads to an increase in App Number of crashes
  • Support only Kotlin
  • The default is through HashMap cache ID Waste space , Although it can be done at the module level build.gradle Add in file
    defaultCacheImplementation = “SPARSE_ARRAY” To modify the default implementation as SparseArray

ViewBinding Solve the above problems , But it also brings other disadvantages :

  • ViewBinding Compared with kotlinx.android.synthetic It is used in a complicated way
  • stay Activity 、 Fragment 、Dialog 、 Adapter in ViewBinding and DataBinding
    The initialization method is somewhat different It needs to be dealt with separately include belt merge The layout of the label , And without merge Layout of labels, etc
  • DataBinding combination LiveData When used together, it needs to be treated separately

But for view binding , There are also convenient ways to write , Come and see it .

Use

First, in the Gradle The plug-in declares the use of

// Android Studio 3.6
android {
    
    viewBinding {
    
        enabled = true
    }
    dataBinding{
    
        enabled = true
    }
}

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

secondly , For the convenience of one line of code, directly use ViewBinding, We also need to introduce

implementation 'com.hi-dhl:binding:1.0.5'

And then directly Activity、Fragment、Adapter Where it needs to be used

class HomeFragment : Fragment() {
    

	// One line of code ,FragmentHomeBinding  Is the corresponding Fragment or Activity Auto corresponding Binding, It has something to do with your own name 
    private val binding: FragmentHomeBinding by viewbind()
    private var count = 0

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    
        super.onViewCreated(view, savedInstanceState)
        //click by xml Medium id
        binding.click.setOnClickListener {
    
            count++
            //dashboardView by xml Medium id
            binding.dashboardView.setPointer(count % 21)
        }
    }
}

Layout file xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:context=".fragment.HomeFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.kxqin.myapplication.view.DashboardView
            android:id="@+id/dashboardView"
            android:layout_width="match_parent"
            android:layout_height="300dp" />

        <TextView
            android:id="@+id/click"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Click"
            android:textColor="@color/black"
            android:layout_gravity="center" />
    </LinearLayout>

</FrameLayout>

After that, just like kotlin Plug-ins work like that binding Call corresponding id That's it , Is it a line of code to use directly viewbinding 了 ? Is it convenient ? Hurry up kotlin Replace the plug-in before it is deactivated .

版权声明
本文为[KIDD-4869]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603464556.html