gogoWebsite

Jetpack series replacement of ButterKnife's new view tool Detailed explanation of the use of ViewBinding

Updated to 2 days ago

In Android development, based on the problem of getting the layout control findViewById, there are some frameworks to avoid repeated meaningless operations. But with the upgrade of Android system, popular libraries such as ButterKnife also stopped updating. Of course, some new frameworks are also emerging that are convenient for us to use, such as ViewBinding in the Jetpack series provided by Google.

 

Configure ViewBinding

The function of ViewBinding is very simple, just to avoid writing findViewById, and the configuration work is also very simple. If your Android studio version is in 3.6 or higher then you can use it.

Just add the following configuration to your project module:

// Android studio version is in 3.6 and above
 android {
     // Requires Android Gradle Plugin 3.6.0
     viewBinding {
         enabled = true
     }
 }

 //The Android Studio version is in 4.0 and above
 android {
     buildFeatures {
         viewBinding true
     }
 }

This way the preparations are done and can be used in the project.

 

Use of ViewBinding

For the use of ViewBinding, we use Activity, Fragment, Adapter, etc.

Of course, ViewBinding is very simple no matter where we are used. After we configure it, Android Studio will automatically layout the corresponding Binding class for each of our layout files. The naming rule for generating Binding classes is to rename the layout file in camel mode and add Binding as the ending.

For example, we define an activity_main.xml layout, and the corresponding Binding class is ActivityMainBinding.

Of course, some layout files do not want to generate corresponding Binding classes, so you can add the following statement at the root element of the layout file:

<LinearLayout
    xmlns:andro
    ...
    tools:viewBindingIgnore="true">
    ...
</LinearLayout>

Use view binding in Activity

Our layout file is shown in the figure:

<LinearLayout ... >
    <TextView android: />
    <ImageView android:cropToPadding="true" />
    <Button android:
        android:background="@drawable/rounded_button" />
</LinearLayout>

Then in the Activity interface, we only need to obtain its Binding file and it can be used normally, as shown in the figure:

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        binding = (getLayoutInflater());
        View view = ();
        setContentView(view);
        ("Hello World!");
        (new () {
            
        });
    }

Use view binding in Fragment

The same is true for use in fragment module. By obtaining the Binding file generated according to the layout file, it can be used normally, as shown in the figure:

    private FragmentMainBinding binding;

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

    @Override
    public void onDestroyView() {
        ();
        binding = null;
    }

Using view binding in Adapter

When using Adapter adapter, you only need to use the Binding file generated based on the layout file. Just pay attention to passing values ​​in ViewHolder as shown in the figure:

@Override
     public onCreateViewHolder(ViewGroup parent, int viewType) {
         RecyclerItemAuthMangerBinding binding = ((()) , parent, false);
    
        // View view = (()).inflate(.recycler_item_auth_manger, parent, false);
         RecyclerViewHolder viewHolder = new RecyclerViewHolder(binding);
         return viewHolder;
     }


     public static class RecyclerViewHolder extends {
         TextView mTextView;
         //Previous writing
         //public ViewHolder(@NonNull View itemView) {
         // super(itemView);
         // mTextView = (.tv_include);
         //}

         //Writing using ViewBinding
         public RecyclerViewHolder (@NonNull RecyclerItemAuthMangerBinding binding) {
             super(());
             mTextView = ;
         }
     }

 

The above can basically be used normally in development, and of course there are some special uses such as introducing layout include and merge.

First of all, the include layout. We only need to set an id for the introduced layout when including. Then when we use it, we can directly call the control name of the include layout through this ID, as shown in the figure:

// include layout referenced configuration
     <include
         android: id = "@+id/includeid"
         layout = "@layout/includelayout" />
        
     // When using it, use it directly through the naming ID when referenced.
      = "Title"
      {
     }

Then for merge layout, this method is not a simple reference, because merge removes one layer of layout nesting and does not have a layout wrapper. So we need to add merge layout binding to the main layout for us to use, as shown in the figure:

ActivityMainBinding binding = (layoutInflater)
     //Bind the reference merge layout to the reference layout
     TitlebarBinding titlebarBinding = ()
     setContentView()
      = "Title"
      {
     }

In this way, the use of ViewBinding is introduced.

This is the first article in the Jetpack series. There will be more functions introduced to everyone in the future. Let’s learn and improve together! ! !