【问题标题】:Kotlin: Recycler View ChoppyKotlin: Recycler View Choppy
【发布时间】:2018-11-07 06:29:17
【问题描述】:

我的适配器正确地将图像应用到 RecyclerView 并正确滚动...直到我添加大量项目。然后它变得相当不稳定,我知道这是我的方法的问题。请看下面的代码:

class FragmentMenuViewAdapter(private val menuItems: ArrayList<MenuItemModel>, private val clickListener: (MenuItemModel) -> Unit) : RecyclerView.Adapter<FragmentMenuViewAdapter.CustomViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {

        val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
        StrictMode.setThreadPolicy(policy)

        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.recyclerview_list_items, parent, false)
        return CustomViewHolder(cellForRow)
    }

    override fun getItemCount(): Int {

        return menuItems.size
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {

        var menuItemIconURI = RouteManager.mAppModel?.resourcesURL + menuItems[position].icon

        menuItemIconURI = menuItemIconURI.replace("\$platform", "iOS")
        menuItemIconURI = menuItemIconURI.replace("\$scale", "@3x")

        val inputStream = URL(menuItemIconURI).openStream()
        holder.view.menuButton.setImageBitmap(BitmapFactory.decodeStream(inputStream))
        holder.bind(menuItems[position], clickListener)
    }

class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {

        fun bind(menuItem: MenuItemModel, clickListener: (MenuItemModel) -> Unit) {
            view.setOnClickListener{clickListener(menuItem)}
        }
    }
}

图片正在通过 URL 链接设置。我有它的工作,但我怀疑我在某个地方犯了一个新手错误,或者只是采取了错误的方法。任何关于如何调整我的代码的建议将不胜感激。

【问题讨论】:

    标签: android performance android-recyclerview kotlin


    【解决方案1】:

    我认为问题来自在主线程中解码位图图像。尝试改用Glide。它可以帮助我们在需要时异步加载图像。例如,如果你想快速滚动RecyclerView,它不需要解码和显示所有图像。此外,它还有一个缓存机制,让这个过程更加顺畅。

    build.gradle

    dependencies {
        implementation 'com.github.bumptech.glide:glide:4.7.1'
    }
    

    FragmentMenuViewAdapter.kt

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
    
        var menuItemIconURI = RouteManager.mAppModel?.resourcesURL + menuItems[position].icon
    
        menuItemIconURI = menuItemIconURI.replace("\$platform", "iOS")
        menuItemIconURI = menuItemIconURI.replace("\$scale", "@3x")
    
        Glide.with(holder.view.menuButton.context)
            .load(menuItemIconURI)
            .into(holder.view.menuButton)
    
        holder.bind(menuItems[position], clickListener)
    }
    

    【讨论】:

    • 有趣,我会试一试。将报告;谢谢!
    • 很好的回应,我非常感谢。这绝对完美。干得好!
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 2015-11-14
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多