- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在将 DataBinding 与 RecyclerView 一起使用,当列表首次加载时,我遇到了相当严重的延迟。然而,当我滚动经过一个页面并且它停止创建新的 View 持有者时,一切都很好。
在创建过程中,我唯一要做的就是使用 DataBindingUtils
膨胀布局,所以我想知道是否有可以改进的部分。
下面附有相关代码 fragment 。我当前正在使用库 FastAdapter
,因此签名不会完全匹配
创建 View 持有者时,我执行以下操作:
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
}
看来,对于我更复杂的布局,每个 View 持有者大约需要 30-60 毫秒,导致明显的滞后。
绑定(bind)和解除绑定(bind)就像这样:
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)
基本上,我通常设置一个数据模型,并为每个查看者实现解除绑定(bind)。看起来没有什么问题。
阅读其他文章,似乎大多数开发人员都具有与我相同的 View 持有者创建方法。 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
最佳答案
我做了一些更多的测量,发现罪魁祸首实际上是布局膨胀(即 60 毫秒),而不仅仅是绑定(bind)(即 3 毫秒)。要进行验证,请在没有 DataBindingUtil
的情况下按原样膨胀 View ,然后在 View 上调用 bind
。
从这里开始,问题还出在 MaterialChips 的使用上。将 6 个 Material 芯片切换为 TextView 后,充气时间减少了 3 倍,达到 10-30 毫秒左右。
关于android - 数据绑定(bind)膨胀非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56746436/
我需要使用view.getWidth(),但我不能,因为我还没有膨胀 View 。在使用 view.getWidth() 之前如何膨胀 View ? MainActivity 类: p
这个问题很难尝试和表述,但我会尽力而为。 基本上,我有一个应用程序,我想将代码拆分得更多。为了尝试解释这一点,我将举一个我的屏幕示例。 在我的主屏幕上,我有一个标题、用户详细信息、余额、下一个账单详细
我有一个 TableLayout,我在其中动态添加行,一开始是空的,我想在用户单击它时在该行中加载一个 xml。 我已经给行赋值了OnClick方法,但是不知道在onclick方法中进入时如何加载xm
我使用的是 C++ 中的 libcurl 库,这是一个相当大的库(.lib 文件大约 2MB),项目是用 CURL_STATICLIB 编译的 至于现在我只有一个 .cpp 文件,其中包含 heade
背景: 我正在努力使一堆 PNG 尽可能小。我正在使用诸如 PngOut、PngCrush 和 OptiPng 之类的工具。 问题: 我遇到了一个大小为 1434 KB 但只有 230 x 230 像
我一直在努力找出我的应用程序使用的内存不断增长的原因,直到在 heroku 上的生产中它失败并下降。 经过一些相当广泛的研究,使用 Top 观察 RSIZE 在本地增长并使用 Oink 尝试查明我只是
我有一个使用 C++ 版本的 zlibs deflate 压缩的数据 ArrayBuffer(使用默认值)。由于没有服务器,我现在需要在客户端中增加这些数据。我已经使用 C++ 中的默认 inflat
我的代码有问题: @Override public View getView(int position, View convertView, ViewGroup parent) {
我有一个 fragment ,我像下面一样对其进行膨胀,但它给出了运行时错误: public View onCreateView(LayoutInflater inflater, ViewGroup
我正在学习 Fragment,并且我是通过在 Fragment 中使用 FB Login 来学习的。但我不断收到异常 Error inflateing class com.facebook.login
第一种方法: LinearLayout parent = ...; View child = LayoutInflator.inflate(context, parent, true); 第二种方法:
我刚刚完成了一个站点构建,在该站点构建中,我们必须将文件提供给第 3 方以与他们的系统集成。构建必须是独立的,因为会有多方部署它们,能力水平各不相同。 我遇到的问题是,在下载我的 Javascript
我正在寻找一种方法来“扩充”Android XML 布局,这在编译时是未知的。我已经搜索了几个小时,总能找到答案,这是不可能的,因为 LayoutInflater 不能使用简单的 XML 文件。好的,
我有一个从 Faragment 扩展而来的类,叫做 Agenda。它的布局有一个 listView,但是当这个类返回到主 Activity 时,我得到了一个错误。这是错误和代码: > 03-11 13
我的应用程序一启动就崩溃了。我收到以下错误: android.view.InflateException: Binary XML file line #34: Error inflating clas
我想膨胀 R.id.catText,但如果我自己膨胀它,它永远不会显示。如果我inflate R.id.assets(容器),那么两个元素都会正常显示。我只是不想要容器。如何在不膨胀 R.id.ass
我尝试创建自定义 ViewGroup 类,但是当我使用方法 findViewById() 时它返回 null,但展开 View 是可以的。 代码是: public class HorizontalLi
我正在尝试创建一个在屏幕上滑动的 textView,但它会产生错误,但我不知道为什么。这是我的 xml: 还有我的 Activity 课: package com.exampl
我正在构建一个应用程序,我在其中使用了 glomadrain 的动画切换按钮。完成编码后出现以下错误: at com.android.internal.os.ZygoteInit.main(Zygot
我只是想按照 android dev 上的示例代码来膨胀 Activity 中的 fragment 。我有一个更复杂的项目正在工作,但我遇到了与这里的简单项目相同的错误。谁能指出我正确的方向? 扩展
我是一名优秀的程序员,十分优秀!