Android 第一个(4-5)卷轴的RecyclerView延迟
我已经为我的项目使用RecyclerView创建了自己的CalendarYearView列表。它在最初的几卷(4-5卷)中落后,但在那之后速度更快了。我无法找出问题所在 我尝试添加Android 第一个(4-5)卷轴的RecyclerView延迟,android,performance,android-recyclerview,Android,Performance,Android Recyclerview,我已经为我的项目使用RecyclerView创建了自己的CalendarYearView列表。它在最初的几卷(4-5卷)中落后,但在那之后速度更快了。我无法找出问题所在 我尝试添加setHasFixedSize(true)和sethasTableId(true)。但这两种情况都不起作用 这在所有的设备上都发生过,我试过三星S8、moto G3和Xiomi-MI-Pad 我在下面附上了我的文件,给我一些解决这个问题的建议 年度列表活动 class YearsListActivity : BaseA
setHasFixedSize(true)
和sethasTableId(true)
。但这两种情况都不起作用
这在所有的设备上都发生过,我试过三星S8、moto G3和Xiomi-MI-Pad
我在下面附上了我的文件,给我一些解决这个问题的建议
年度列表活动
class YearsListActivity : BaseActivity(), SimpleYearCallback {
private var currYear: Int = 0
private var currPos: Int = 0
var l: ArrayList<Int> = ArrayList()
private var mAdapter: YearsRecyclerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_years_list)
init()
}
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
setTitle(getString(R.string.tit_choose_year), false)
}
override fun onStart() {
super.onStart()
refresh()
}
override fun init() {
mAdapter = YearsRecyclerAdapter(this, l)
mAdapter?.callback = this
val llm = LinearLayoutManager(this)
llm.orientation = RecyclerView.VERTICAL
recycler?.layoutManager = llm
val snapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(recycler)
recycler?.adapter = mAdapter
}
override fun refresh() {
l.clear()
for (i in GAuth.getInstance().minYear..GAuth.getInstance().maxYear) {
l.add(i)
}
currYear = CalendarUtil.calendar().get(Calendar.YEAR)
currPos = currYear - GAuth.getInstance().minYear
recycler?.scrollToPosition(currPos)
mAdapter?.notifyDataSetChanged()
}
override fun onMonthClicked(month: Int, year: Int) {
ActivityManager.goHomeActivity(this, CalendarUtil.calendar(year, month).timeInMillis)
}
}
布局/活动\u年份\u列表
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent">
<include
layout="@layout/view_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="@layout/view_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar_container"/>
</android.support.constraint.ConstraintLayout>
您可能需要告知您在哪个设备上遇到此问题(处理器、RAM、操作系统版本)。这种情况是否发生在多个设备中?您已经运行UI性能测试了吗?如果是这样的话,你用过什么工具?它发生在所有的设备上,我试过三星S8、moto G3和Xiomi-PadIMO,出现这种延迟是因为你在onBindViewHolder
中循环。据我所知,该方法不适用于处理许多高活动/流程。也许您可以尝试将数据处理转移到其他函数。使用set和get进行简单的设置try@MarioMargoPradiptaonBindViewHolder绑定12个月的数据。我清空了onBindViewHolder并运行了代码,但在第一个4-5项滚动中仍然很慢
class SimpleMonthView : LinearLayout, ViewInitializer {
var millis: Long = 0
var l: ArrayList<String>? = ArrayList()
var cal: Calendar? = null
var title: CustomTextView? = null
private var isCurrentMonth = false
private var todayDate: Int? = -1
constructor(context: Context)
: super(context) {
initView()
}
constructor(context: Context, attrs: AttributeSet?)
: super(context, attrs) {
initView()
}
override fun initAttrs(attrs: AttributeSet?) {
}
override fun initView() {
View.inflate(context,
R.layout.view_simple_month_view_static,
this)
title = findViewById(R.id.title)
}
override fun refresh() {
title?.text =
CalendarUtil.getMonthName(CalendarUtil.calendar(millis))
generateGrid()
}
override fun destroy() {
}
private fun generateGrid() {
cal = CalendarUtil.calendar(millis)
if (CalendarUtil.isSameMonth(millis,
CalendarUtil.calendar().timeInMillis)) {
isCurrentMonth = true
todayDate = CalendarUtil.calendar()
.get(Calendar.DAY_OF_MONTH)
}
var count = CalendarData.WEEK
if (CalendarUtil.getMonthStartPos(cal)!! !=
Calendar.SUNDAY)
for (i in 1 until CalendarUtil.getMonthStartPos(cal)!!) {
count++
}
for (i in 1..CalendarUtil.getMonthMaxDays(cal)!!) {
val tv = findViewById<SquareTextView>
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent">
<include
layout="@layout/view_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="@layout/view_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar_container"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/space_4dp"
android:orientation="vertical">
<chuchu.guruji.scheduler.view.CustomTextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/space_4dp"
android:fontFamily="@font/roboto_thin"
android:gravity="center"
android:textColor="@color/year_header"
android:textSize="@dimen/txt_large"
android:textStyle="italic|bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month0"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month1"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month2"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month3"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month4"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month5"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month6"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month7"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month8"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month9"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month10"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
<chuchu.guruji.scheduler.view.calendar.SimpleMonthView
android:id="@+id/month11"
android:paddingStart="@dimen/space_4dp"
android:paddingEnd="@dimen/space_4dp"
style="@style/SimpleMonthView.EqWidth" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText">
<chuchu.guruji.scheduler.view.CustomTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:fontFamily="@font/roboto_bold"
android:gravity="center_vertical"
android:paddingStart="@dimen/space_4dp"
android:textSize="@dimen/txt_tiny" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day0"
style="@style/CustomTextView.Thin"
android:text="S" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day1"
style="@style/CustomTextView.Thin"
android:text="M" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day2"
style="@style/CustomTextView.Thin"
android:text="T" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day3"
style="@style/CustomTextView.Thin"
android:text="W" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day4"
style="@style/CustomTextView.Thin"
android:text="T" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day5"
style="@style/CustomTextView.Thin"
android:text="F" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day6"
style="@style/CustomTextView.Thin"
android:text="S" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day7"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day8"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day9"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day10"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day11"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day12"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day13"
style="@style/CustomTextView.Thin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day14"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day15"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day16"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day17"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day18"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day19"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day20"
style="@style/CustomTextView.Thin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day21"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day22"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day23"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day24"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day25"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day26"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day27"
style="@style/CustomTextView.Thin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day28"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day29"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day30"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day31"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day32"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day33"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day34"
style="@style/CustomTextView.Thin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day35"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day36"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day37"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day38"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day39"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day40"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day41"
style="@style/CustomTextView.Thin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day42"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day43"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day44"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day45"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day46"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day47"
style="@style/CustomTextView.Thin" />
<chuchu.guruji.scheduler.view.SquareTextView
android:id="@+id/day48"
style="@style/CustomTextView.Thin" />
</LinearLayout>
</LinearLayout>
<style name="CustomTextView.Thin">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:textSize">@dimen/txt_tiny</item>
<item name="fontFamily">@font/roboto_light</item>
<item name="android:textColor">@color/default_txt_color</item>
</style>