gpt4 book ai didi

android - Kotlin:回收站 View 断断续续

转载 作者:行者123 更新时间:2023-11-29 23:28:03 29 4
gpt4 key购买 nike

我的适配器正确地将图像应用到 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 链接设置的。我让它工作,但我怀疑我在某个地方犯了新手错误,或者只是采取了错误的方法。非常感谢任何有关如何调整我的代码的建议。

最佳答案

我认为问题出在主线程中解码位图图像。尝试使用 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)
}

关于android - Kotlin:回收站 View 断断续续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53184544/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com