1. The background
In most cases, replace findViewById with ViewBinding. In the past, if you didn’t use a third-party framework, no matter how encapsulated, it was still very inconvenient to use (such as unable to find controls, control ID conflicts, etc.)
2. Start the ViewBinding process
- Update AS to version 3.6 first
- Open the Gradle file for Module
- Add the following code to the Android function and you’re done
android {
...
viewBinding {
enabled = true
}
}
Copy the code
3. Use of ViewBinding
- By default, the layout XML file generates an XXXBinding class
- Tools :viewBindingIgnore=”true”**
<LinearLayout
...
tools:viewBindingIgnore="true" >
...
</LinearLayout>
Copy the code
- Add a layout file activity_main.xml as follows:
<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code
-
You can then use the XXXBinding class within the Activity, which provides three initialization functions
fun inflate(inflater: LayoutInflater): XXXBinding fun inflate(inflater: LayoutInflater, parent: ViewGroup, attachToParent: boolean): XXXBinding fun bind(view: View): XXXBinding Copy the code
-
Add an activityMainActivity.kt with the following code
class MainActivity:BaseActivity() { private val tag:String ="MainActivity" private lateinit var viewbinding:ActivityMainBinding private lateinit var button:Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d(tag, "onCreate:$SPLASH_TIME") } override fun observerViewModel() { } override fun initViewBinding() { viewbinding = ActivityMainBinding.inflate(layoutInflater) setContentView(viewbinding.root) } override fun initViews() { button = viewbinding.buttonMain button.setOnClickListener { navigationLoginActivity() } } private fun navigationLoginActivity(){ startActivity(Intent(this,LoginActivity::class.java)) } } Copy the code
-
4. Use the ViewBinding on the Fragment
-
Add a layout file called fragment_view_binding.xml. The code is not listed:
-
Add a Fragment, ViewBindingFragment, the code is as follows:
public class ViewBindingFragment extends Fragment { private FragmentViewBindingBinding binding; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { binding = FragmentViewBindingBinding.inflate(inflater, container, false); View view = binding.getRoot(); Binding. TvContent. SetText (" here is set up in fragments through ViewBinding text "); return view; } public static ViewBindingFragment newInstance() { Bundle args = new Bundle(); ViewBindingFragment fragment = new ViewBindingFragment(); fragment.setArguments(args); return fragment; } @Override public void onDestroyView() { super.onDestroyView(); binding = null; }}Copy the code
Precautions for using Fragment
When using ViewBinding in your Fragment, remove the binding reference in the **onDestroyView()** method. Otherwise, you will leak memory.
Reference: www.jianshu.com/p/e2c565d4d…