数据绑定 inflation 非常慢
Data Binding inflation is very slow
我目前正在将 DataBinding 与 RecyclerView 结合使用,并且在首次加载列表时出现严重的延迟。但是,在我滚动到一个页面并且它停止创建新的查看器之后,一切都很好。
在创建过程中,我唯一做的就是使用 DataBindingUtils
扩充布局,所以我想知道是否有可以改进的部分。
下面附有相关代码片段。我目前正在使用库 FastAdapter
,因此签名不会完全匹配
在创建 viewholder 时,我执行以下操作:
override fun createView(ctx: Context, parent: ViewGroup?): View {
val start = System.nanoTime()
val binding: ViewDataBinding = DataBindingUtil.inflate(
LayoutInflater.from(ctx),
layoutRes, parent,
false,
null
)
L.d { "Create view ${(System.nanoTime() - start) / 1000000}" }
return binding.root
}
似乎对于我更复杂的布局,每个 viewholder 大约需要 30-60 毫秒,导致明显的滞后。
绑定和解绑是这样的:
final override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) {
super.bindView(holder, payloads)
val binding = DataBindingUtil.getBinding<Binding>(holder.itemView) ?: return
binding.bindView(holder, payloads)
binding.executePendingBindings()
}
open fun Binding.bindView(holder: ViewHolder, payloads: MutableList<Any>) {
setVariable(BR.model, data)
}
final override fun unbindView(holder: ViewHolder) {
super.unbindView(holder)
val binding = DataBindingUtil.getBinding<Binding>(holder.itemView) ?: return
binding.unbindView(holder)
binding.unbind()
}
open fun Binding.unbindView(holder: ViewHolder) {}
final override fun getViewHolder(v: View): ViewHolder = ViewHolder(v, layoutRes)
基本上,我通常有一个我设置的单一数据模型,并且我为每个 viewholder 实现自己解除绑定。那里似乎没有问题。
阅读其他文章,似乎大多数开发人员的viewholder 创建方法与我相同。 DataUtilBinding 不是要在创建时完成,还是我遗漏了什么?
作为补充,这是我的相关布局xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="model"
type="github.fragment.ShortRepoRowItem" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:windowBackground"
android:foreground="?selectableItemBackground"
android:paddingStart="@dimen/kau_activity_horizontal_margin"
android:paddingTop="@dimen/kau_padding_small"
android:paddingEnd="@dimen/kau_activity_horizontal_margin"
android:paddingBottom="@dimen/kau_padding_small"
tools:context=".activity.MainActivity">
<TextView
android:id="@+id/repo_name"
style="@style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{model.name}"
android:textColor="?android:textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@tools:sample/full_names" />
<TextView
android:id="@+id/repo_desc"
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="@{model.description}"
android:textColor="?android:textColorSecondary"
app:goneFlag="@{model.description}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/repo_name"
tools:text="@tools:sample/lorem/random" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_stars"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_star_border"
app:compactNumberText="@{model.stargazers.totalCount}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="123" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_forks"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_fork"
app:compactNumberText="@{model.forkCount}"
app:layout_constraintStart_toEndOf="@id/repo_stars"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="123" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_issues"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_issues"
app:compactNumberText="@{model.issues.totalCount}"
app:layout_constraintStart_toEndOf="@id/repo_forks"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="1.5k" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_prs"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_pull_requests"
app:compactNumberText="@{model.pullRequests.totalCount}"
app:layout_constraintStart_toEndOf="@id/repo_issues"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="123" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_language"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{model.primaryLanguage.name}"
app:chipIcon="@drawable/ic_language"
app:languageColor="@{model.primaryLanguage.color}"
app:layout_constraintEnd_toStartOf="@id/repo_date"
app:layout_constraintStart_toEndOf="@id/repo_prs"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.25"
tools:text="JavaScript" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_date"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/repo_language"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:relativeDateText="@{model.pushedAt}"
app:textStartPadding="4dp"
tools:text="@tools:sample/date/mmddyy" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我试过用两个线性布局而不是 ConstraintLayout 做同样的事情,但它似乎没有太大区别。
代码快照:https://github.com/AllanWang/GitDroid/tree/f802c991580d70470b422186fc43f46b9cfe2465
我做了更多测量,发现罪魁祸首实际上是布局 inflation(即 60 毫秒),而不仅仅是绑定(即 3 毫秒)。
要验证,请在没有 DataBindingUtil
的情况下扩充视图,然后在视图上调用 bind
。
从那里开始,问题也出在 MaterialChips 的使用上。
将 6 material 个芯片切换到文本视图后,inflation 时间缩短了 3 倍,大约为 10-30 毫秒。
我目前正在将 DataBinding 与 RecyclerView 结合使用,并且在首次加载列表时出现严重的延迟。但是,在我滚动到一个页面并且它停止创建新的查看器之后,一切都很好。
在创建过程中,我唯一做的就是使用 DataBindingUtils
扩充布局,所以我想知道是否有可以改进的部分。
下面附有相关代码片段。我目前正在使用库 FastAdapter
,因此签名不会完全匹配
在创建 viewholder 时,我执行以下操作:
override fun createView(ctx: Context, parent: ViewGroup?): View {
val start = System.nanoTime()
val binding: ViewDataBinding = DataBindingUtil.inflate(
LayoutInflater.from(ctx),
layoutRes, parent,
false,
null
)
L.d { "Create view ${(System.nanoTime() - start) / 1000000}" }
return binding.root
}
似乎对于我更复杂的布局,每个 viewholder 大约需要 30-60 毫秒,导致明显的滞后。
绑定和解绑是这样的:
final override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) {
super.bindView(holder, payloads)
val binding = DataBindingUtil.getBinding<Binding>(holder.itemView) ?: return
binding.bindView(holder, payloads)
binding.executePendingBindings()
}
open fun Binding.bindView(holder: ViewHolder, payloads: MutableList<Any>) {
setVariable(BR.model, data)
}
final override fun unbindView(holder: ViewHolder) {
super.unbindView(holder)
val binding = DataBindingUtil.getBinding<Binding>(holder.itemView) ?: return
binding.unbindView(holder)
binding.unbind()
}
open fun Binding.unbindView(holder: ViewHolder) {}
final override fun getViewHolder(v: View): ViewHolder = ViewHolder(v, layoutRes)
基本上,我通常有一个我设置的单一数据模型,并且我为每个 viewholder 实现自己解除绑定。那里似乎没有问题。
阅读其他文章,似乎大多数开发人员的viewholder 创建方法与我相同。 DataUtilBinding 不是要在创建时完成,还是我遗漏了什么?
作为补充,这是我的相关布局xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="model"
type="github.fragment.ShortRepoRowItem" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:windowBackground"
android:foreground="?selectableItemBackground"
android:paddingStart="@dimen/kau_activity_horizontal_margin"
android:paddingTop="@dimen/kau_padding_small"
android:paddingEnd="@dimen/kau_activity_horizontal_margin"
android:paddingBottom="@dimen/kau_padding_small"
tools:context=".activity.MainActivity">
<TextView
android:id="@+id/repo_name"
style="@style/TextAppearance.AppCompat.Medium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{model.name}"
android:textColor="?android:textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@tools:sample/full_names" />
<TextView
android:id="@+id/repo_desc"
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="@{model.description}"
android:textColor="?android:textColorSecondary"
app:goneFlag="@{model.description}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/repo_name"
tools:text="@tools:sample/lorem/random" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_stars"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_star_border"
app:compactNumberText="@{model.stargazers.totalCount}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="123" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_forks"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_fork"
app:compactNumberText="@{model.forkCount}"
app:layout_constraintStart_toEndOf="@id/repo_stars"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="123" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_issues"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_issues"
app:compactNumberText="@{model.issues.totalCount}"
app:layout_constraintStart_toEndOf="@id/repo_forks"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="1.5k" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_prs"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_pull_requests"
app:compactNumberText="@{model.pullRequests.totalCount}"
app:layout_constraintStart_toEndOf="@id/repo_issues"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.12"
tools:text="123" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_language"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{model.primaryLanguage.name}"
app:chipIcon="@drawable/ic_language"
app:languageColor="@{model.primaryLanguage.color}"
app:layout_constraintEnd_toStartOf="@id/repo_date"
app:layout_constraintStart_toEndOf="@id/repo_prs"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:layout_constraintWidth_percent="0.25"
tools:text="JavaScript" />
<com.google.android.material.chip.Chip
android:id="@+id/repo_date"
style="@style/RepoChips"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/repo_language"
app:layout_constraintTop_toBottomOf="@id/repo_desc"
app:relativeDateText="@{model.pushedAt}"
app:textStartPadding="4dp"
tools:text="@tools:sample/date/mmddyy" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我试过用两个线性布局而不是 ConstraintLayout 做同样的事情,但它似乎没有太大区别。
代码快照:https://github.com/AllanWang/GitDroid/tree/f802c991580d70470b422186fc43f46b9cfe2465
我做了更多测量,发现罪魁祸首实际上是布局 inflation(即 60 毫秒),而不仅仅是绑定(即 3 毫秒)。
要验证,请在没有 DataBindingUtil
的情况下扩充视图,然后在视图上调用 bind
。
从那里开始,问题也出在 MaterialChips 的使用上。 将 6 material 个芯片切换到文本视图后,inflation 时间缩短了 3 倍,大约为 10-30 毫秒。