android中旋转视图
The Android development environment has been based on three types of files since the beginning: XML, Kotlin, and Java. XML files contain everything related to design, and Kotlin/Java files include anything other than the design part.
从一开始,Android开发环境就基于三种文件类型:XML,Kotlin和Java。 XML文件包含与设计相关的所有内容,而Kotlin / Java文件包含除设计部分以外的所有内容。
Eventually, UI and business logic needed to be linked, and that’s what this article is all about. Let’s see how finding a view in the layout is evolved over the years.
最终,需要将UI和业务逻辑链接起来,这就是本文的全部内容。 让我们看看多年来如何在布局中查找视图。
'findViewByID' (‘findViewByID’)
findViewByID is the first one in the line, and it was introduced in API Level 1 by the Android team. This function provides a view object as a return type and developers link this object to the view created in the View class.
findViewByID是该行中的第一个,由Android团队在API Level 1中引入。 此函数提供一个视图对象作为返回类型,开发人员将此对象链接到在View类中创建的View 。
Every view in Android is extended from the View class. Thus, all the views used in the XML layout, one way or another, extends to the View class. findViewByID returns an instance of that class.
Android中的每个视图都是从View类扩展的。 因此,XML布局中使用的所有视图都以某种方式扩展到View类。 findViewByID返回该类的实例。
class ExampleActivity extends Activity {
TextView title;
TextView subtitle;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
title = (TextView) findViewById(R.id.title)
subtitle = (TextView) findViewById(R.id.subtitle)
}
}Recently, the Android team made an enhancement to this function to overcome view casting. So the developers no longer need to cast the views while linking with findViewByID.
最近,Android团队对该功能进行了增强,以克服视图转换问题。 因此,开发人员在与findViewByID链接时不再需要投射视图。
Despite the efforts to enhance findViewByID, many developers felt it wasn’t the best approach. So people started creating alternatives like Butter Knife, and at some point, the Android team came up with data binding (though its primary focus isn’t only to link the views).
尽管为增强findViewByID所做的努力,但许多开发人员仍认为这不是最佳方法。 因此人们开始创建Butter Knife之类的替代产品,并且在某个时候,Android团队提出了数据绑定的方法(尽管其主要重点不仅在于链接视图)。
牛油刀 (Butter Knife)
The Android Butter Knife library is a lightweight view0injection library that works on annotations. It was one of the first libraries that seemed to be a successful alternative to the traditional findViewByID.
Android Butter Knife库是适用于注释的轻量级view0injection库。 它是第一个似乎可以成功替代传统findViewByID 。
It was created by Jake Wharton, and you can find the documentation on how to use Butter Knife on its official website. This library links the views from a layout using the@BindView annotation. For a while, developers used it as a sort of formal alternative to findViewByID. Let’s take a look at how to use it:
它由Jake Wharton创建,您可以在其官方网站上找到有关如何使用黄油刀的文档。 该库使用@BindView批注从布局链接视图。 一段时间以来,开发人员将其用作findViewByID的一种正式替代方案。 让我们看一下如何使用它:
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
}
}It’s almost similar to findViewByID; the only benefit for developers in terms of removing boilerplate code is that we don’t need to implement the declaration and initialization of the views separately.
它几乎与findViewByID类似; 对于开发人员而言,删除样板代码的唯一好处是,我们不需要分别实现视图的声明和初始化。
数据绑定 (Data Binding)
The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format, rather than programmatically.
数据绑定库是一个支持库,可让您使用声明性格式(而不是通过编程方式)将布局中的UI组件绑定到应用程序中的数据源。
The Data Binding Library generates binding classes for the layouts, which we can use in Android components like Activity and Fragment. Data binding is a sort of declarative solution with type and null safety.
数据绑定库为布局生成绑定类,我们可以在Android组件(例如Activity和Fragment 。 数据绑定是一种具有类型和null安全性的声明性解决方案。
To make a layout support data binding, we have to wrap the content of the file with the layout tag. This will generate a binding file of that layout with a reference to all of the views in it. Have a look:
为了使布局支持数据绑定,我们必须使用layout标签包装文件的内容。 这将生成该布局的绑定文件,并引用其中的所有视图。 看一看:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintLayout... /> <!-- UI layout's root element -->
</layout>Once we’ve finished that part in the XML, a binding class will be generated, and we need to use it in the View class. This binding-file instance contains all of the views in the layout. Have a look:
在XML中完成该部分后,将生成一个绑定类,并且需要在View类中使用它。 该绑定文件实例包含布局中的所有视图。 看一看:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(
this, R.layout.activity_main)
binding.tvName.text = "John"
}This seems to be a reasonable solution, but data binding is created to solve more complicated problems than just linking views, such as the layout file accessing Data classes to publish data from the XML itself and loading remote images using binding adapters.
这似乎是一个合理的解决方案,但是创建数据绑定不仅仅是解决链接视图的问题,例如,布局文件访问Data类以从XML本身发布数据并使用绑定适配器加载远程图像。
These sort of things made Data Binding a complicated library to solve the view-linking problem. By nature, data binding is created to solve complicated issues because it creates a longer compile time and also errors are hard to understand.
这些事情使数据绑定成为一个复杂的库,以解决视图链接问题。 从本质上讲,创建数据绑定是为了解决复杂的问题,因为它会创建更长的编译时间,而且错误也很难理解。
Kotlin合成 (Kotlin Synthetics)
When Kotlin was introduced for Android development, it solved many problems, including view linking. Kotlin Extensions is a plugin by the Kotlin team. It eliminates the need to use findViewById in your code.
当Kotlin引入Android开发时,它解决了许多问题,包括视图链接。 Kotlin Extensions是Kotlin团队的插件。 它消除了在代码中使用findViewById的需要。
With the help of Kotlin Extensions, synthetic code came into existence in Android, which solves the view-linking problem in Android without using findViewByID. Unlike other libraries here, we can use the ID of the view directly in class files.
借助Kotlin Extensions,合成代码在Android中应运而生,它无需使用findViewByID即可解决Android中的视图链接问题。 与这里的其他库不同,我们可以在类文件中直接使用视图的ID。
Kotlin synthetics invokes thefindViewById function the first time and then caches the view instances in a HashMap by default. This cache configuration can be changed to SparseArray or no cache via the Gradle settings.
Kotlin合成器首次调用findViewById函数,然后默认情况下将视图实例缓存在HashMap 。 可以通过Gradle设置将此缓存配置更改为SparseArray或不缓存。
Kotlin synthetics is one of the best approaches — it’s type-safe, and we can use the IDs directly from the layout. By using the ? operator from Kotlin, we can make this process null-safe.
Kotlin合成器是最好的方法之一-它是类型安全的,我们可以直接从布局中使用ID。 通过使用? 来自Kotlin的运算符,我们可以使此过程为null安全。
Apart from the benefits, we have a problem here — if we use an ID of the view, Android Studio shows autocomplete, even if the layout that’s inflated is different from the one that has the view you’re trying toaccess. In such cases, if you don’t use the ? operator, your app will crash.
除了带来的好处之外,我们这里还有一个问题-如果我们使用视图ID,即使膨胀的布局与尝试访问视图的布局不同,Android Studio也会显示自动完成功能。 在这种情况下,如果您不使用? 运算符,您的应用将崩溃。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_name?.text = "John"
}视图绑定 (View Binding)
Finally ViewBinding, is an official solution to link views to class files. The idea behind ViewBinding is to create a binding class for every layout that holds the references of the views in the layout. This reduces boilerplate code in your components (Activity and Fragment), and we can access views by creating an instance to that class.
最后, ViewBinding是将视图链接到类文件的官方解决方案。 ViewBinding的思想是为每个布局创建一个绑定类,该绑定类保存布局中视图的引用。 这减少了组件中的样板代码( Activity和Fragment ),并且我们可以通过为该类创建实例来访问视图。
With view binding, you no longer need to do the null check on a view while performing actions on it, just like in data binding. As the binding class generated directly from the layout file, it only contains the views within the layout.
使用视图绑定,就像在数据绑定中一样,在对视图执行操作时,您不再需要对视图执行null检查。 作为直接从布局文件生成的绑定类,它仅包含布局中的视图。
Type-safety is one of the most significant advantages in view binding. If you try to assign a text watcher to a button, it won’t accept it because the Binding class contains the reference with appropriate view types.
类型安全是视图绑定中最重要的优点之一。 如果您尝试将文本观察器分配给按钮,则该按钮将无法接受,因为Binding类包含具有适当视图类型的引用。
View binding’s sole purpose is to replace the use of findviewbyId. Unlike data binding, it’s lightweight so it won’t take much time for compilation.
视图绑定的唯一目的是代替使用findviewbyId 。 与数据绑定不同,它是轻量级的,因此不需要太多时间进行编译。
class MainActivity : AppCompatActivity() {
private lateinit var activityMainBinding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
val rootView = activityMainBinding.root
setContentView(rootView)
}
}奖金 (Bonus)
To learn more about Android advanced development, read the following articles:
要了解有关Android高级开发的更多信息,请阅读以下文章:
“How to Integrate Google Pay Into Your Existing Android App”
That is all for now — hope you learned something useful. Thanks for reading.
目前仅此而已-希望您学到了有用的东西。 谢谢阅读。
翻译自: https://medium.com/better-programming/the-evolution-of-view-linking-in-android-d6219678740d
android中旋转视图