当前位置:网站首页>Recycleview lazy load failure (II)
Recycleview lazy load failure (II)
2022-06-21 19:58:00 【yufumatou】
Preface : Following 《RecycleView Lazy load failure 》 after , Let's introduce another scenario , Here's the picture . Two layers of RecycleView, The outer layer is a vertical list style , The inner layer is grid style , Because the number of inner grids is not fixed , Inner layer required RecycleView The root layout height of is wrap_content( notes : This is what causes the inner layer RecycleView Causes of lazy loading failure )

problem : Even though the 2 The item shows only a small part , But all the data is still loaded , Inner lazy loading failure , When there is a large amount of internal data , Load all at once , I'll feel carton .
analysis : First , Analyze why the outer layer RecycleView Lazy loading normal , And the inner RecycleView Failed ? The difference is that the outer layer RecycleView The height is certain , At least not more than the parent control , The inner root layout height is wrap_content, namely item There is no limit on the maximum height , As big as the inner layer needs to be , So all the inner data is loaded , We can first set the root layout height of the inner layer to a fixed value to verify , It is found that the inner layer also supports lazy loading after the height is fixed , After all RecycleView Height limits , Exceeding the height of item It doesn't load , But this does not meet our needs , When there is little internal data, it will be left blank , The display is incomplete when there is too much data , And only the outer layer responds to sliding Events , The inner layer cannot respond , So we can only set the inner root layout height to wrap_content.
1、 Outer adapter
class TestPercentAdapter : RecyclerView.Adapter<TestPercentAdapter.TestPercentViewHolder>(){
private val testPercentList = arrayListOf<TestPercentBean>()
init {
for (i in 1..20){
testPercentList.add(TestPercentBean(" Outer layer ${i} term "))
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestPercentViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test, parent, false)
return TestPercentViewHolder(view)
}
override fun getItemCount(): Int {
return testPercentList.size
}
override fun onBindViewHolder(holder: TestPercentViewHolder, position: Int) {
holder.tvItem.text = testPercentList[position].mTitle
holder.adapterTwo.testChildrenList = testPercentList[position].mChildrenList
holder.adapterTwo.notifyDataSetChanged()
Log.e("aa", "***************${testPercentList[position].mTitle}")
}
inner class TestPercentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvItem = view.findViewById<TextView>(R.id.tv_item)
val rvItem = view.findViewById<RecyclerView>(R.id.rv_item)
val adapterTwo = TestChildrenAdapter()
init {
rvItem.layoutManager = GridLayoutManager(rvItem.context, 2)
rvItem.adapter = adapterTwo
}
}
}2、 Outer layout
<?xml version="1.0" encoding="utf-8"?>
<com.hualala.myapplication.MyLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:textColor="@color/colorAccent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.hualala.myapplication.MyLinearLayout>3、 Inner adapter
class TestChildrenAdapter : RecyclerView.Adapter<TestChildrenAdapter.TestChildrenViewHolder>(){
var testChildrenList = arrayListOf<TestPercentBean.TestChildrenBean>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestChildrenViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_test_two, parent, false)
return TestChildrenViewHolder(view)
}
override fun getItemCount(): Int {
return testChildrenList.size
}
override fun onBindViewHolder(holder: TestChildrenViewHolder, position: Int) {
holder.tv_test.text = testChildrenList[position].mTitle
Log.e("aa", "***********${testChildrenList[position].mTitle}")
}
class TestChildrenViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tv_test = itemView.findViewById<TextView>(R.id.tv_test)
}
}4、 Interior layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_test"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:textColor="@color/colorPrimaryDark"/>5、 Home page layout
<?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"/>
<View
android:layout_width="1px"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#666666"/>
<Button
android:id="@+id/bt_update"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/rv_test"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
app:layout_constraintEnd_toEndOf="parent"
android:gravity="center"
android:text=" to update "/>
</androidx.constraintlayout.widget.ConstraintLayout>6、 Load data
rv_test.layoutManager = LinearLayoutManager(this)
rv_test.adapter = TestPercentAdapter()
bt_update.setOnClickListener { (rv_test.adapter as TestPercentAdapter).notifyDataSetChanged() }8、 Entity class
class TestPercentBean constructor(title: String){
val mTitle = title
val mChildrenList = ArrayList<TestChildrenBean>()
init {
for (i in 1..(Math.random() * 100).toInt()){
mChildrenList.add(TestChildrenBean(" Inner layer {$i} term "))
}
}
class TestChildrenBean constructor(title: String){
val mTitle = title
}
}programme : The reason is analyzed , We have a preliminary plan , These two conditions need to be met to solve :1、 To the interior RecycleView Set the maximum height to outer layer RecycleView Height ( Solve lazy loading , The maximum number of loaded pieces is limited , Not infinite ) 2、 Slide conflict ( This part is too complicated , This led to the eventual abandonment of the scheme , Think about what I clicked on in the first item RecycleView Slide up and up , Since it has been shown that there is no need to load any more, the event needs to be handed over to the outer layer RecycleView, But Dangdi 2 Intranuchal layer RecycleView When all are displayed , And send the event to the 2 Intranuchal layer RecycleView Continue loading the remaining data , After all are displayed, the event is handed over to the outer layer RecycleView—— Inner layer RecycleView The outer layer responds to sliding when the layout is not fully displayed , If all are displayed, the inner layer will respond , If the inner layer slides to the 1item Or last item, Then the outer layer will respond )
Simple plan : Consider the complexity of the above scheme , Then change your mind , With single layer RecycleView Realization , You need the grid to support dynamic allocation of columns , When it is outer data, it is 1 Column , For inner layer data, it is 2 Column .
adopt GridLayoutManager.SpanSizeLookup Realize the dynamic number of columns
1、GridLayoutManager The first 2 Parameters spanCount Is the least common multiple of the number of rows and columns
2、getSpanSize The return value is spanCount Divide by the number of rows
1、 Adapter
class TestAdapter : RecyclerView.Adapter<TestAdapter.TestViewHolder>() {
var testList = ArrayList<TestBean>()
init {
for (i in 1..20) {
testList.add(TestBean(" Outer layer ${i} term ", true))
for (j in 1..(Math.random() * 100).toInt()) {
testList.add(TestBean(" Inner layer ${j} term ", false))
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.item_test_two, parent, false)
return TestViewHolder(view)
}
override fun getItemCount(): Int {
return testList.size
}
override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
holder.tv_test.text = testList[position].mTitle
if (testList[position].mIsPercent){
holder.tv_test.setTextColor(holder.tv_test.resources.getColor(R.color.colorAccent))
}else{
holder.tv_test.setTextColor(holder.tv_test.resources.getColor(R.color.colorPrimaryDark))
}
Log.e("aa", "***********${testList[position].mTitle}")
}
class TestViewHolder constructor(view: View) : RecyclerView.ViewHolder(view) {
val tv_test = itemView.findViewById<TextView>(R.id.tv_test)
}
}2、 Item layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_test"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:textColor="@color/colorPrimaryDark"/>3、 Entity class
class TestBean(title: String, isPercent: Boolean) {
val mTitle = title
val mIsPercent = isPercent
}4、 Load data
/* 1、GridLayoutManager The first 2 Parameters spanCount Is the least common multiple of the number of rows and columns
* 2、getSpanSize The return value is spanCount Divide by the number of rows
* */
val gridLayoutManager = GridLayoutManager(this, 2)
val testAdapter = TestAdapter()
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){
override fun getSpanSize(position: Int): Int {
return if (testAdapter.testList[position].mIsPercent) 2 else 1
}
}
rv_test.layoutManager = gridLayoutManager
rv_test.adapter = testAdapter
bt_update.setOnClickListener { (rv_test.adapter as TestAdapter).notifyDataSetChanged() }
边栏推荐
- DataGear 使用坐标映射表制作地理坐标数据可视化看板
- [comprehensive pen test] difficulty 2.5/5: "tree array" and "double tree array optimization"
- 508. Most Frequent Subtree Sum
- linux-mysql-命令
- Gradle download and installation configuration
- inno setup 安装路径框学习
- 記一些PAT題目(一)
- Flink 系例 之 TableAPI & SQL 與 示例模塊
- LeetCode个人题解(剑指offer 21-25)21. 调整数组顺序使奇数位于偶数前面,22. 链表中倒数第k个节点,24. 反转链表,25. 合并两个排序的链表
- The R language catiols package divides the data, randomforest package constructs the random forest model, uses the importance function to calculate the importance of each feature in the random forest
猜你喜欢

API interface for discharge summary identification - medical bill OCR identification / discharge diagnosis record / electronic medical record / claim settlement service

How to temporarily modify samesite=none and secure in Chrome browser

How to set the picture background to transparent

Flink 系例 之 TableAPI & SQL 與 示例模塊

JVM内存结构

W10 add system environment variable path

汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(三)——俄罗斯方块详细设计

尚硅谷 尚硅谷 | 什么是ClickHouse表引擎 Memory和Merge

API de table & SQL et module d'échantillon pour le système Flink
![2022年下半年深圳地区数据分析师认证(CPDA),[进入查看]](/img/9a/4fe513a71f5efc7cce318708d6931e.jpg)
2022年下半年深圳地区数据分析师认证(CPDA),[进入查看]
随机推荐
Startup mode of Jupiter notebook and related problems
After the 80 version of Google browser, how to deal with the problem samesite cross domain problem
理财产品到期当日能赎回吗?
Jupyter Notebook启动方式及相关问题
Qt Creator 7.0常见问题和常见用法
论文解读(USIB)《Towards Explanation for Unsupervised Graph-Level Representation Learning》
How to set the picture background to transparent
jvm造轮子
机器学习之贝叶斯分类与集成学习
HMS core machine learning service ID card identification function to achieve efficient information entry
动态规划【二】(线性dp)
如何在Chrome浏览器中临时修改SameSite=None和Secure
The R language uses the follow up The plot function visualizes the longitudinal follow-up chart of multiple ID (case) monitoring indicators, and uses line Col parameter custom curve color (color)
R语言caTools包进行数据划分、randomForest包构建随机森林模型、使用importance函数计算随机森林模型中每个特征的重要度、varImpPlot函数可视化特征的重要度
R语言使用plyr包的rbind.fill函数纵向合并两个数据列不同的dataframe数据
2022年6月25日PMP考试通关宝典-3
在 KubeSphere 上部署 Apache Pulsar
Kubernetes 跨 StorageClass 迁移 Persistent Volumes 完全指南
W10添加系统环境变量Path
Novice uses apiccloud visual development to build the mall home page