Android view binding merge

Долгожданный View Binding в Android

Пару дней назад Google выпустил Android Studio 3.6 Canary 11, главным нововведением в которой стал View Binding, о котором было рассказано еще в мае на Google I/O 2019.

View Binding — это инструмент, который позволяет проще писать код для взаимодейтсвия с view. При включении View Binding в определенном модуле он генерирует binding классы для каждого файла разметки (layout) в модуле. Объект сгенерированного binding класса содержит ссылки на все view из файла разметки, для которых указан android:id .

Как включить

Чтобы включить View Binding в модуле надо добавить элемент в файл build.gradle :

Также можно указать, что для определенного файла разметки генерировать binding класс не надо. Для этого надо указать аттрибут tools:viewBindingIgnore=»true» в корневой view в нужном файле разметки.

Как использовать

Каждый сгенерированный binding класс содержит ссылку на корневой view разметки ( root ) и ссылки на все view, которые имеют id. Имя генерируемого класса формируется как «название файла разметки», переведенное в camel case + «Binding».

Например, для файла разметки result_profile.xml :

Будет сгенерирован класс ResultProfileBinding , содержащий 2 поля: TextView name и Button button . Для ImageView ничего сгенерировано не будет, как как оно не имеет id . Также в классе ResultProfileBinding будет метод getRoot() , возвращающий корневой LinearLayout .

Чтобы создать объект класса ResultProfileBinding , надо вызвать статический метод inflate() . После этого можно использовать корневой view как content view в Activity :

Позже binding можно использовать для получения view:

Отличия от других подходов

Главные преимущества View Binding — это Null safety и Type safety.

При этом, если какая-то view имеется в одной конфигурации разметки, но отсутствует в другой ( layout-land , например), то для нее в binding классе будет сгенерировано @Nullable поле.

Также, если в разных конфигурациях разметки имеются view с одинаковыми id, но разными типами, то для них будет сгенерировано поле с типом android.view.View .
(По крайней мере, в версии 3.6 Canary 11)

А вообще, было бы удобно, если бы сгенерированное поле имело наиболее возможный специфичный тип. Например, чтобы для Button в одной конфигурации и TextView в другой генерировалось поле с типом TextView ( public class Button extends TextView ).

При использовании View Binding все несоответствия между разметкой и кодом будут выявляться на этапе компиляции, что позволит избежать ненужных ошибок во время работы приложения.

Использование в RecyclerView.ViewHolder

Ничего не мешает использовать View Binding при создании view для RecyclerView.ViewHolder :

Однако, для создания такого ViewHolder придется написать немного бойлерплейта:

Было бы удобнее, если при работе с RecyclerView.ViewHolder метод inflate(. ) не будет иметь параметр layoutInflater , а будет сам получать его из передаваемого parent .

Тут нужно ещё упомянуть, что при использовании View Binding поиск view через findViewById() производится только один раз при вызове метода inflate() . Это дает преимущество над kotlin-android-extensions , в котором кеширование view по умолчанию работало только в Activity и Fragment , а для RecyclerView.ViewHolder требовалась дополнительная настройка.

В целом, View Binding это очень удобная вещь, которую легко начать использовать в существующих проектах. Создатель Butter Knife уже рекомендует переключаться на View Binding.

Немного жаль, что такой инструмент не появился несколько лет назад.

Источник

View Binding in Android

We have learnt that every time we need to access a view from our XML layout into our Java or Kotlin code, we must use findViewById(). It was okay for small/personal projects where we use 5 to 6 views in a layout. But for larger projects we have comparatively more views in a layout, and accessing each view using the same findViewById() is not really comfortable.

What is View Binding?

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module

Simply put, this allows us to access the views from the layout file in a very simple way by linking variables of our Kotlin or Java code with the XML views. When a layout is inflated, it creates a Binding object, which contains all the XML views that are casted to the correct type. This makes it really easier for us since we can retrieve all views in one line of code.

Читайте также:  Titanium backup для андроида

Getting started with View Binding

  1. Let’s start by enabling view binding in our project:

In build.gradle(:app) add the code in -> android

2. Before the onCreate() method, we create our binding object

3. Perform the following steps in onCreate() method:

  • Call the inflate() method to create an instance of the binding class for the activity to use.
  • Get reference to the root view
  • Pass the root view to setContentView() [setContentView(binding.root)] instead of layout [setContentView(R.id.activity_main)]

4. To get reference of any view, we can use the binding object:

Using View Binding in Fragments

We follow the same steps:

  1. Before the onCreateView() method, we create our binding object

2. Initialize our binding object in onCreateView()

3. To get reference of any view, we can use the binding object

Note: The name of the binding class is generated by converting the name of the XML file to camel case and adding the word “Binding” to the end. Similarly, the reference for each view is generated by removing underscores and converting the view name to camel case . For example, activity_main.xml becomes ActivityMainBinding, and you can access @id/text_view as binding.textView.

View binding has important advantages over using findViewById():

  • Null safety: Since view binding creates direct references to views, there’s no risk of a null pointer exception due to an invalid view ID.
  • Type safety: The fields in each binding class have types matching the views they reference in the XML file. This means that there’s no risk of a class cast exception.

Start using View Binding

If you’re intrigued by View Binding and want to learn more about it, here’s some resources for you to learn:

One-Liner ViewBinding Library

You would have noticed that to use View Binding, we need to call the static inflate() method included in the generated binding class ( which creates an instance of the binding class for the activity or fragment )

Yesterday I came across an awesome library that makes ViewBinding one-liner ( By removing the boilerplate code and easily set ViewBindings with a single line )

One-liner ViewBinding Library : [Click Here]

Источник

View Binding: Merge

View Binding was introduced in 2019 and is really nice. I have written about it before. However I recently discovered a small gotcha that is easy to work around, but it did me a frighten when I first encountered it, and is not, at the time of writing, mentioned in the official documentation. In this post we’ll look at and understand the issue; and then look at three different, but all quite simple, approaches to dealing with it.

The way in which I encountered the issue was a little odd, and it was very much a corner case that caused it. I was working on the sample code project for another blog post and there was only a very simple layout as the focus of the subject matter was code-based rather than layouts, and a simple layout would suffice. The Activity layout that was auto-created by Android Studio was a ConstaraintLayout containing a simple TextView . As I was tidying up the code I realised that ConstraintLayout was a little overkill so I decided to replace it with a far more lightweight FrameLayout . So far, so good. When I ran lint, I get a MergeRootFrame warning which was correct because the FrameLayout could actually be replaced with a . block, which I did. We’ll discuss why this is a good change shortly.

Everything was now working perfectly when I realised that I hadn’t enabled View Binding on the project. I promptly did so, and as I made the necessary changes to the onCreate() method of my Activity I found that the signature of the inflate() method of the generated binding class was not what I expected, and certainly didn’t match the example in the official docs.

Читайте также:  Как сделать календарь для андроида

After much head scratching I realised that the use of the merge root tag resulted in a different signature for the inflate() method of the generated binding because it now required an additional ViewGroup argument.

This actually makes perfect sense if we consider what actually does. For any Activity there is a ContentFrameLayout with an id of android.R.id.content . When we call setContentView() the layout we specify gets inflated in to that parent. When I had a FrameLayout at the root of my layout this would have added the FrameLayout and its child TextView as children of this ContentFrameLayout . The FrameLayout itself is actually serving no purpose because the ContentFrameLayout itself is essentially a FrameLayout . If we use instead, then during inflation the itself is ignored, and the TextView will be added directly to the ContentFrameLayout , which flattens our layout hierarchy slightly. This is a good thing.

When we call setContentView() with a layout resource ID, then that method adds the children of the merge to the ContentFrameLayout automagically for us because it is a method of Activity which knows about the ContentFrameLayout . Using View Binding requires us to first inflate the layout using the inflate() method of the generated binding class, and then use a different variant of setContentView() which takes a View argument rather than a layout resource id. So when the binding is generated for a layout with a root it has no knowledge of the parent in to which the children of the should be added and therefore requires a ViewGroup argument which is not required for other layout root elements.

To show this in code, when we don’t have a layout with a root we do this:

Источник

From Kotlin synthetics to Android ViewBinding: the definitive guide

Binding your views like a pro in 2 minutes.

Since Kotlin 1.4.20, JetBrains deprecated the kotlin-android-extensions plugin in favor of Android View Binding solution.

If you have been using this plugin, it’s time to move on before this feature is completely removed (or switch to Compose?).

For having completely refactored our Android application to use View Binding, we know that it can be somehow painful and sometimes it looks less simple with this new solution. For example, the binding of your view will have to be done differently if you’re working with an Activity or a Fragment. If your layout uses include there are some tricky ways to handle them too.

That’s why we’ve been implementing some convenient methods to make it simpler.

Setup

If you’re refactoring your whole application code, we advise you to do it module per module to avoid committing too many updates.

Enable view binding

To enable the ViewBinding feature, you just need to enable this build feature on your module build.gradle files:

Remove android extensions plugin

If you’ve been using kotlin-android-extensions before, remove the plugin from the build.gradle file too:

Don’t forget to also remove the Android Extensions experimental flag:

Parcelize annotations

The Parcelize annotations have been moved to another package & the previous one has been deprecated.

So, if you used the Kotlin Parcelize annotation to automatize the implementation of your Parcelable objects, you’ll need to use the new plugin :

Then, replace imports in your code from kotlinx.android.parcel to kotlinx.parcelize.

Activities

To set up an Activity to use your binding class you basically have to:

1- Declare a late init variable for your binding class.

2- In the onCreate method, inflate your layout using the static inflate method from the generated Binding class.

3- Call the Activity setContentView with the root view from your binding class.

We can definitely make it way shorter using some extensions:

Our viewBinding method is a simple delegate that inflates lazily the view of the activity by using the inflater method given as a parameter.

In the Activity we just need to declare the binding as a simple val property and set the content view of the activity.

Читайте также:  Видеоняня для ios или android

Fragments

Using View Binding in Fragments is quite less easy as you have to do many things to make it works:

  1. 1- Declare a var property for your binding class instance.
  2. In the onCreateView callback, inflate the layout for your binding class & return the root view of it.
  3. To avoid potential leaks, reset your binding class to null in the onDestroyView callback.

As per the official documentation, it should look like that:

That’s a lot of code to add & you should also pay attention to potential leaks on the binding class. This can also be disturbing as the method is quite different from using it in an Activity. Note that you also need to use the !! operator to hide the nullability of the original field.

There’s a lot of room for improvement here and I found a way better solution to deal with it in 1 line:

Yep, really simple!🚀

Here are some explanations:

  1. We pass to the Fragment constructor the id of our fragment layout, which will be inflated for us.
  2. We declare the binding property that will handle the fragment lifecycle work for us.

Have a look at this Gist:

Our viewBindingWithBinder simply instantiate a delegate object that will bind the view of our fragment lazily. As the Fragment already inflate the view from the layout id that we pass to it in its constructor, we just need to bind our binding class from it.

The FragmentAutoClearedValueBinding class is the one that will be responsible to clear the reference to the binding class when the fragment gets destroyed by adding an observer to its lifecycle. In the getValue method, we bind the view to our binding class the first time we access our binding property from our fragment.

ViewHolder

If you’ve been using Kotlin synthetics, you probably have something that looks like this:

You can now remove the usage of LayoutContainer and make some updates in your view holder:

Here we have simply changed our constructor to take the binding class in parameter instead of having a view object.

To instantiate your view holder, you can create a static method in a companion object:

Includes

In your layouts, you probably have views that are included in your layout using the include tag. Then, you must be aware that it can be tricky to deal with it with view binding.

With a view group

If your included view has a root view that is a ViewGroup (LinearLayout, FrameLayout, etc.) then you can set an id to the include tag in your main layout.

The generated LayoutBinding class will have a property mergeLayout of type LayoutMergeBinding that reference the layout_mege layout views.

So in an activity, you can easily reference your TextView as:

With a

If your included view uses a tag in it like in this example:

And your main layout is:

Then, the layout_merge.xml file content will be merged into your main layout. But, the binding class will not expose a property titleLbl (it will only contain your root LinearLayout ) 😭.

So, what can you do to access to your titleLbl ? You’ll need to re-bind your view with another binding class. As each layout files have its own binding class, your layout_merge layout will have a LayoutMergeBinding class generated. That’s the one we will use to retrieve our views.

From an activity, you can add another property for the 2nd binding class:

The mergeBinding property will then allow you to access the titleLbl view.

Conclusion

We hope this article was useful to you if you’re still refactoring your views to use ViewBinding or to give you some hints to refactor how you’re using it.

And you, how did you implement it into your app? Don’t hesitate to share with us your tip&tricks!

If you want to join our Bureau of Technology or any other Back Market department, take a look here, we’re hiring! 🦄

Источник

Оцените статью