Android ViewPager2嵌套RecyclerView性能优化RecycledViewPool,kotlin

Android ViewPager2嵌套RecyclerView性能优化RecycledViewPool,kotlin



import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.RecycledViewPool
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val pager = findViewById(R.id.vp2)
        pager.adapter = MyPageAdapter(this)

        //如果是offscreenPageLimit=1,如果有三个Fragment(1,2,3),当前显示1,则第三个Fragment:3在不可见时候状态消失
        pager.offscreenPageLimit = 2
    }

    class MyPageAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
        var mRecycledViewPool = RecycledViewPool()
        var mFragments = arrayListOf()

        init {
            mRecycledViewPool.setMaxRecycledViews(3, 500)

            for (i in 0 until 3) {
                mFragments.add(MyFragment(i))
            }
        }

        override fun getItemCount(): Int {
            return mFragments.size
        }

        override fun createFragment(position: Int): Fragment {
            return mFragments[position]
        }
    }

    class MyFragment(private var seq: Int) : Fragment() {
        var mPool: RecycledViewPool? = null

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            var recyclerView = RecyclerView(inflater.context)
            var layoutManager = GridLayoutManager(inflater.context, 4)
            recyclerView.adapter = MyRecyclerViewAdapter(seq)
            recyclerView.layoutManager = layoutManager


            layoutManager.isItemPrefetchEnabled = true //Android源代码中默认true
            layoutManager.recycleChildrenOnDetach = true
            recyclerView.setRecycledViewPool(mPool)
            recyclerView.setHasFixedSize(true)

            return recyclerView
        }
    }

    class MyRecyclerViewAdapter(private var seq: Int) : RecyclerView.Adapter() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
            var view = LayoutInflater.from(parent.context)
                .inflate(android.R.layout.simple_list_item_2, parent, false)
            return MyVH(view)
        }

        override fun getItemCount(): Int {
            return Int.MAX_VALUE
        }

        override fun onBindViewHolder(holder: MyVH, position: Int) {
            holder.text1.text = "F-$seq"
            holder.text2.text = "$position"
        }
    }

    class MyVH(itemView: View) : ViewHolder(itemView) {
        var text1: TextView
        var text2: TextView

        init {
            text1 = itemView.findViewById(android.R.id.text1)
            text2 = itemView.findViewById(android.R.id.text2)
        }
    }
}

Android ViewPager2嵌套RecyclerView性能优化RecycledViewPool,kotlin_第1张图片

ViewPager2和Fragment可见性及懒加载解决方案_viewpager2 懒加载_zhangphil的博客-CSDN博客ViewPager2和Fragment新的可见性及懒加载解决方案,旨在解决ViewPager(ViewPager2)只有当前Fragment唯一可见时候网络刷新或绘图。注意:过去的setUserVisibleHint()已被废弃,开发者不要再使用基于setUserVisibleHint()解决Fragment可见性和懒加载(或延迟加载)的场景。新方案用最新的ViewPager2替代过去旧版的ViewPager,再结合Fragment的resume和pause生命周期实现。写一个demo例子,测试的主类Mhttps://blog.csdn.net/zhangphil/article/details/108893237

你可能感兴趣的:(kotlin,Android,android,kotlin)