ViewStub vs View.GONE vs Inflate vs ViewSwitcher
ViewStub vs View.GONE vs Inflate vs ViewSwitcher
我有一个与延迟加载视图/布局性能相关的问题。
有时我们想动态显示/隐藏多个视图。在这种情况下,我们有四种选择:
- 在 .xml 中包含所有布局并使用 setVisibility 方法(如 View.GONE 和 VIEW.VISIBLE)
- 使用 ViewFlipper/ViewSwitcher
- 使用 ViewStub
- 以编程方式扩充新布局。
哪个表现最好?
我一直在谷歌搜索,发现 ViewStub 是专门为此设计的,但我不确定。也许我错了,或者甚至还有我不知道的第五种选择。您对此有不同的看法或经验吗?谢谢!
这取决于您要自行膨胀的视图。您提到的每种方法都有其自身的开销,您需要决定在何处妥协。
- 如果您的视图非常简单并且不需要进行太多初始化,只需将其设置为
View.GONE
。如果它相当复杂或布局更好,请不要这样做。
ViewFlipper
和 ViewSwitcher
旨在在不同视图之间设置动画。它的目的不是显示和隐藏单个视图。如果你有不同的视图在不同的时间显示在同一个地方。
ViewStub
只是一个占位符,它用更复杂的布局替换了自己。
- 手动完成所有操作就像在没有布局信息的情况下使用
ViewStub
。如果您需要以编程方式创建或设置视图,这可能是一个不错的选择。
2017 年,Android 团队在支持库 v24 上发布了 AsyncLayoutInflater。 2020 年是 of Jetpack library.
的一部分
AsyncLayoutInflater 是另一种延迟扩充布局的方法。正如 API 文档所说
Helper class for inflating layouts asynchronously. To use, construct
an instance of AsyncLayoutInflater on the UI thread and call
inflate(int, ViewGroup, OnInflateFinishedListener). The
AsyncLayoutInflater.OnInflateFinishedListener will be invoked on the
UI thread when the inflate request has completed.
This is intended for parts of the UI that are created lazily or in
response to user interactions. This allows the UI thread to continue
to be responsive & animate while the relatively heavy inflate is being
performed.
这是使用 AsyncLayoutInflater 的代码片段:
button.setOnClickListener { view ->
val container = findViewById(R.id.container)
val asyncLayoutInflater = AsyncLayoutInflater(this@MainActivity)
asyncLayoutInflater.inflate(
R.layout.view_sample,
container,
object : OnInflateFinishedListener() {
fun onInflateFinished(view: View, resId: Int, parent: ViewGroup) {
parent.addView(view)
}
}
)
}
我有一个与延迟加载视图/布局性能相关的问题。 有时我们想动态显示/隐藏多个视图。在这种情况下,我们有四种选择:
- 在 .xml 中包含所有布局并使用 setVisibility 方法(如 View.GONE 和 VIEW.VISIBLE)
- 使用 ViewFlipper/ViewSwitcher
- 使用 ViewStub
- 以编程方式扩充新布局。
哪个表现最好? 我一直在谷歌搜索,发现 ViewStub 是专门为此设计的,但我不确定。也许我错了,或者甚至还有我不知道的第五种选择。您对此有不同的看法或经验吗?谢谢!
这取决于您要自行膨胀的视图。您提到的每种方法都有其自身的开销,您需要决定在何处妥协。
- 如果您的视图非常简单并且不需要进行太多初始化,只需将其设置为
View.GONE
。如果它相当复杂或布局更好,请不要这样做。 ViewFlipper
和ViewSwitcher
旨在在不同视图之间设置动画。它的目的不是显示和隐藏单个视图。如果你有不同的视图在不同的时间显示在同一个地方。ViewStub
只是一个占位符,它用更复杂的布局替换了自己。- 手动完成所有操作就像在没有布局信息的情况下使用
ViewStub
。如果您需要以编程方式创建或设置视图,这可能是一个不错的选择。
2017 年,Android 团队在支持库 v24 上发布了 AsyncLayoutInflater。 2020 年是 of Jetpack library.
的一部分AsyncLayoutInflater 是另一种延迟扩充布局的方法。正如 API 文档所说
Helper class for inflating layouts asynchronously. To use, construct an instance of AsyncLayoutInflater on the UI thread and call inflate(int, ViewGroup, OnInflateFinishedListener). The AsyncLayoutInflater.OnInflateFinishedListener will be invoked on the UI thread when the inflate request has completed.
This is intended for parts of the UI that are created lazily or in response to user interactions. This allows the UI thread to continue to be responsive & animate while the relatively heavy inflate is being performed.
这是使用 AsyncLayoutInflater 的代码片段:
button.setOnClickListener { view ->
val container = findViewById(R.id.container)
val asyncLayoutInflater = AsyncLayoutInflater(this@MainActivity)
asyncLayoutInflater.inflate(
R.layout.view_sample,
container,
object : OnInflateFinishedListener() {
fun onInflateFinished(view: View, resId: Int, parent: ViewGroup) {
parent.addView(view)
}
}
)
}