当前位置:网站首页>Recycleview lazy load failure

Recycleview lazy load failure

2022-06-21 19:58:00 yufumatou

Preface : When the amount of data is large ( Such as 200 More than ) Obviously feel APP Carton , Through investigation, it is found that RecycleView Adapter's onBindViewHolder Execute as many times as you have data , Sliding display lazy load failed .

reason :RecycleView Or the parent control is used horizontally android:layout_weight="1" Property will cause RecycleView Lazy loading is invalid

<?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(" The first {$i} term ")
        }
    }

    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()

programme : about RecycleView Need percentage in horizontal direction , Don't use “LinearLayout + layout_weight” programme , have access to “ConstraintLayout+layout_constraintWidth_percent” Instead of

<?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>

Last : There is another failure case 《RecycleView Lazy load failure ( Two )》

原网站

版权声明
本文为[yufumatou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211811198065.html