gpt4 book ai didi

android - glide recyclerview 集成有哪些进展?

转载 作者:IT老高 更新时间:2023-10-28 13:35:25 24 4
gpt4 key购买 nike

我刚刚尝试使用 glide recyclerview 集成并阅读了有关它的文档,它说:“RecyclerView 集成库使 RecyclerViewPreloader 在您的应用程序中可用。RecyclerViewPreloader 可以在用户滚动之前自动加载图像在 RecyclerView 中”,但我没有意识到 glide recyclerview 集成和仅 glide 之间有什么区别,请解释一下 glide recyclerview 集成的进步是什么?我怎样才能看到差异?

这是我的代码:

GlideModule.kt

@GlideModule
class GlideModule : AppGlideModule() {
override fun applyOptions(context: Context?, builder: GlideBuilder?) {
val requestOp = RequestOptions.noAnimation()
.priority(Priority.LOW)
builder?.setDefaultRequestOptions(requestOp)
?.setLogLevel(Log.VERBOSE)
super.applyOptions(context, builder)
}

// Disable manifest parsing to avoid adding similar modules twice.
override fun isManifestParsingEnabled(): Boolean {
return false
}
}

MyPreloadModelProvide.kt

class MyPreloadModelProvide(val listUrls: List<String>, val context: Context) : PreloadModelProvider<Any> {
override fun getPreloadItems(position: Int): MutableList<Any> {
val url = listUrls.get(position)
if (TextUtils.isEmpty(url)) {
return Collections.emptyList();
}
return Collections.singletonList(url);
}

override fun getPreloadRequestBuilder(url: Any?): RequestBuilder<*>? {
return GlideApp.with(context)
.load(url)
}

}

MyAdapter.kt

class MyAdapter(val listUrl: List<String>, val context: Context) : RecyclerView.Adapter<MyViewHolder>() {
override fun getItemCount(): Int = listUrl.size

@SuppressLint("CheckResult")
override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {

GlideApp.with(context)
.load(listUrl[position])
.into(holder?.imageView)

holder?.imageView?.setOnClickListener { Toast.makeText(context, listUrl[position], Toast.LENGTH_LONG).show() }
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder = MyViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.item, parent, false))
}

class MyViewHolder(view: View?) : RecyclerView.ViewHolder(view) {
var imageView: ImageView

init {
imageView = view!!.findViewById(R.id.img)
}
}

MainActivity.kt

class MainActivity : AppCompatActivity() {

private lateinit var preloadSizeProvider: ViewPreloadSizeProvider<Any>

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

// glide
var listUrls = listOf(
"https://img.pokemondb.net/artwork/bulbasaur.jpg",
"https://img.pokemondb.net/artwork/ivysaur.jpg",
"https://img.pokemondb.net/artwork/komala.jpg",
"https://img.pokemondb.net/artwork/turtonator.jpg",
"https://img.pokemondb.net/artwork/togedemaru.jpg",
"https://img.pokemondb.net/artwork/mimikyu.jpg",
"https://img.pokemondb.net/artwork/nihilego.jpg",
"https://img.pokemondb.net/artwork/buzzwole.jpg",
"https://img.pokemondb.net/artwork/pheromosa.jpg",
"https://img.pokemondb.net/artwork/xurkitree.jpg",
"https://img.pokemondb.net/artwork/celesteela.jpg",
"https://img.pokemondb.net/artwork/kartana.jpg",
"https://img.pokemondb.net/artwork/guzzlord.jpg",
"https://img.pokemondb.net/artwork/necrozma.jpg",
"https://img.pokemondb.net/artwork/magearna.jpg",
"https://img.pokemondb.net/artwork/marshadow.jpg"
)

preloadSizeProvider = ViewPreloadSizeProvider<Any>()
val modelProvider = MyPreloadModelProvide(listUrls, this)
val preloader = RecyclerViewPreloader(GlideApp.with(this), modelProvider, preloadSizeProvider, 2 /*maxPreload*/)

// recycler view
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.setHasFixedSize(true)
recycler_view.adapter = MyAdapter(listUrls, this)

// THERE ARE NO DIFFERENCES IF I COMMENT THIS LINE
recycler_view.addOnScrollListener(preloader)
}
}

如果我评论此行,则没有区别recycler_view.addOnScrollListener(preloader)

最佳答案

RecyclerView 集成库使 RecyclerViewPreloader 在您的应用程序中可用。
RecyclerViewPreloader 可以在用户滚动 RecyclerView 的位置之前自动加载图像。

结合正确的图像大小和有效的磁盘缓存策略,该库可以通过确保用户即将到达的图像已经在其中来显着减少用户在滚动图像列表时看到的加载图 block /指示器的数量内存。

要使用 RecyclerView 集成库,请在 build.gradle 文件中添加对它的依赖:

compile ("com.github.bumptech.glide:recyclerview-integration:4.4.0") {
/*Excludes the support library
because it's already included by Glide.*/
transitive = false
}

关于android - glide recyclerview 集成有哪些进展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47663985/

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