Android glide recyclerview集成有哪些进展?

Android glide recyclerview集成有哪些进展?,android,android-recyclerview,kotlin,android-glide,recyclerview-layout,Android,Android Recyclerview,Kotlin,Android Glide,Recyclerview Layout,我刚刚尝试使用glide recyclerview integration并阅读了一份关于它的文档,它说:“recyclerview集成库使你的应用程序中可以使用RecyclerViewReloader。RecyclerViewReloader可以在用户滚动recyclerview之前自动加载图像”,但我没有意识到glide recyclerview集成和仅glide集成之间的任何区别,请解释glide recyclerview集成的进步是什么?我怎么能看出区别呢 这是我的密码: GlideMo

我刚刚尝试使用glide recyclerview integration并阅读了一份关于它的文档,它说:“recyclerview集成库使你的应用程序中可以使用RecyclerViewReloader。RecyclerViewReloader可以在用户滚动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
    }
}
mypreload.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)
    }

}
类MyPrelodModelProvider(val-listUrls:List,val-context:context):PrelodModelProvider{
覆盖项目(位置:Int):可变列表{
val url=listUrls.get(位置)
if(TextUtils.isEmpty(url)){
返回集合。emptyList();
}
返回集合。singletonList(url);
}
覆盖有趣的getPreloadRequestBuilder(url:Any?):RequestBuilder{
返回GlideApp.with(上下文)
.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)
    }
}
classmyadapter(val-listUrl:List,val-context:context):RecyclerView.Adapter(){
重写getItemCount():Int=listUrl.size
@SuppressLint(“检查结果”)
覆盖BindViewHolder(holder:MyViewHolder?,位置:Int){
GlideApp.with(上下文)
.load(listUrl[位置])
.into(支架?.imageView)
holder?.imageView?.setOnClickListener{Toast.makeText(上下文,listUrl[position],Toast.LENGTH\u LONG).show()}
}
覆盖fun onCreateViewHolder(父级:ViewGroup?,viewType:Int):MyViewHolder=MyViewHolder(LayoutFlater.from(父级?.context)。充气(R.layout.item,父级,false))
}
类MyViewHolder(视图:视图?):RecyclerView.ViewHolder(视图){
var-imageView:imageView
初始化{
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)
    }
}
class MainActivity:AppCompatActivity(){
私有lateinit变量preloadSizeProvider:ViewPreloadSizeProvider
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//滑翔
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()
val modelProvider=MyPreloadModelProvider(列表URL,此)
val Preload=RecycleServicewPreLoader(GlideApp.with(this),modelProvider,preloadSizeProvider,2/*maxPreload*/)
//回收者视图
回收器\u view.layoutManager=LinearLayoutManager(此)
recycler_视图。setHasFixedSize(真)
recycler_view.adapter=MyAdapter(列表URL,此)
//如果我评论这一行,没有区别
recycler\u view.addOnScrollListener(预加载程序)
}
}
如果我评论这一行,没有区别
recycler\u视图。addOnScrollListener(预加载程序)

RecyclerView
集成库使
RecyclerViewPreloader在应用程序中可用。
而且,
RecyclerViewPreloader
可以在用户在
RecyclerView
中滚动之前自动加载图像

结合正确的映像大小和有效的磁盘缓存策略,此库可以通过确保用户即将访问的映像已在内存中,从而显著减少用户在滚动查看映像列表时看到的加载磁贴/指示器的数量

要使用
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
}

假设用户正在滚动加载大图像的RecyclerView,那么用户应该等待加载图像的延迟,然后才能在相应的列表项中看到它。RecyclerViewPreloader预先加载图像,因此当用户滚动时,已经加载并立即显示图像


在代码中,您使用的是一个小列表和一个小的maxPreload值,因此您不会注意到使用RecycleServiceWPReloader的区别。要查看它的实际效果,您应该使用一个包含许多不同图像的长列表,并使用和不使用预加载程序快速滚动。如果没有预加载程序,在加载之前,您将看到空图像。使用预加载程序,图像应快速、平滑地显示。

谢谢您的回答,我如何才能看到差异?请在慢速网络连接或2G网络上使用您的应用程序,这样您才能看到差异。我理解您所说的内容,但这不取决于网络类型,因为当打开glide的log
setLogLevel(log.VERBOSE)
时,会看到它加载相同数量的请求。感谢您的回答,但这还不够。添加依赖项是不够的