简体   繁体   English

Kotlin:Recycler View Choppy

[英]Kotlin: Recycler View Choppy

My adapter is applying images correctly to the RecyclerView and scrolls properly... until I add a large amount of items.我的适配器正确地将图像应用到 RecyclerView 并正确滚动......直到我添加了大量项目。 It then becomes fairly choppy and I know it's an issue with my approach.然后它变得相当不稳定,我知道这是我的方法的问题。 See the code below:请参阅下面的代码:

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

The image(s) are being set via URL links.图像是通过 URL 链接设置的。 I have it working, but I suspect I'm making a novice mistake somewhere, or am simply taking the wrong approach.我让它工作了,但我怀疑我在某个地方犯了一个新手错误,或者只是采取了错误的方法。 Any advice on how to adjust my code would be much appreciated.任何关于如何调整我的代码的建议将不胜感激。

I think the problem comes from decoding bitmap image in main thread.我认为问题来自在主线程中解码位图图像。 Try to use Glide instead.尝试改用Glide It helps us to load images asynchronously when needed.它可以帮助我们在需要时异步加载图像。 For example if you want to scroll the RecyclerView fast, it doesn't need to decode and show all images.例如,如果你想快速滚动RecyclerView ,它不需要解码和显示所有图像。 In addition it has a caching mechanism to make this process smoother.此外,它还具有缓存机制,可以使此过程更加顺畅。

build.gradle构建.gradle

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

FragmentMenuViewAdapter.kt 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM