当前位置:网站首页>RecycleView懒加载失效问题
RecycleView懒加载失效问题
2022-06-21 18:11:00 【yufumatou】
前言:当数据量比较大(如200条以上)明显感觉到APP卡顿,通过排查发现是RecycleView适配器的onBindViewHolder有多少条数据就执行多少次,滑动显示懒加载失效了。
原因:RecycleView或父控件在水平方向使用android:layout_weight="1"属性则会导致RecycleView懒加载无效
<?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:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_test"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>class TestAdapter : RecyclerView.Adapter<TestAdapter.TestViewHolder>(){
private val testList = arrayListOf<String>()
init {
for (i in 1..500){
testList.add("第{$i}项")
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test, parent, false)
return TestViewHolder(view)
}
override fun getItemCount(): Int {
return testList.size
}
override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
holder.tvItem.text = testList[position]
Log.e("aa", "***********$position")
}
class TestViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvItem = view.findViewById<TextView>(R.id.tv_item)
}
}rv_test.layoutManager = LinearLayoutManager(this)
rv_test.adapter = TestAdapter()方案:对于RecycleView在水平方向需要百分比,不要采用“LinearLayout + layout_weight”方案,可以使用“ConstraintLayout+layout_constraintWidth_percent”代替
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_test"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.5"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintStart_toEndOf="@+id/rv_test"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>最后:还有另一种失效情况《RecycleView懒加载失效问题(二)》
边栏推荐
- TensorFlow 2:使用神经网络对Fashion MNIST分类并进行比较分析
- Write down some pat topics (I)
- Nacos configuration center source code
- 机器学习之绪论
- Ogg-21.3 error reporting ogg-00768 failed to map database character to ulibcharaset
- 机器学习之聚类和降维与度量技术
- 动态规划【二】(线性dp)
- Selection skills of national production reinforced Ethernet switch
- MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级
- C. Helping the Nature(cf)差分
猜你喜欢

gorm数据库是否需要设置外键

一次 MySQL 误操作导致的事故,「高可用」都顶不住了!

How to use devaxpress WPF to create the first MVVM application in winui?

Kubernetes 跨 StorageClass 迁移 Persistent Volumes 完全指南

将图片背景设置为透明的方法介绍

Sword finger offer II 029 Sorted circular linked list

SQL操作:WITH表达式及其应用

Use the uniapp framework to build the zheliban micro application (single sign on, embedded point, aging adaptation, RPC gateway)

Tensorflow 2: use neural network to classify and compare fashion MNIST

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
随机推荐
記一些PAT題目(一)
TensorFlow 2:使用神经网络对Fashion MNIST分类并进行比较分析
机器学习之神经网络与支持向量机
How to temporarily modify samesite=none and secure in Chrome browser
【面试高频题】难度 1.5/5,常见的二分双指针面试题
[comprehensive pen test] difficulty 2.5/5: "tree array" and "double tree array optimization"
[high frequency interview questions] the difficulty is 1.5/5. Common two point double pointer interview questions
[interval and topic prefix sum] line segment tree (dynamic open point) application problem
508. Most Frequent Subtree Sum
【区间和专题の前缀和】线段树(动态开点)运用题
Medical expense list can be entered at a second speed, and OCR recognition can help double the efficiency
Excel文件加密的两种方式
In the same process of go question bank · 9, what is the problem with sending and receiving data at the same time for unbuffered channels
Dynamic programming [II] (linear DP)
力扣今日题1108. IP 地址无效化
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.main参数指定可视化图像标题文本字体的大小
6月25日PMP考前指南,你需要做好这些
How to simulate a request or modify a requested domain name in Chrome browser
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the summary function is used to view the summary statistical information of t
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the coef function is used to obtain the model coefficients and analyze the me