【问题标题】:image change to wrong image in nested recyclerview after scroll滚动后嵌套recyclerview中的图像更改为错误图像
【发布时间】:2020-02-10 02:06:39
【问题描述】:

我用来自https://www.themoviedb.org的api制作了简单的电影目录。

首先我获取流派电影并循环每个流派以获取电影

fun getGenre(){
    index = 0
    dataMovie.clear()
    val Client = Client()
    val apiRoute: Route = Client.getClient()!!.create(Route::class.java)
    apiRoute.getCategoryMovie().enqueue(object :
        Callback<MovieGenresResponse> {
        override fun onResponse(call: Call<MovieGenresResponse>, response: Response<MovieGenresResponse>) {

            if(response.code() == 200) {
                g = response.body().genres
                getMovie()
            }
        }
        override fun onFailure(call: Call<MovieGenresResponse>, t: Throwable){
            Log.e("Ops", t.message)

        }
    })
}

fun getMovie(){
    val Client = Client()
    val apiRoute: Route = Client.getClient()!!.create(Route::class.java)
    apiRoute.getDataMovie(g[index].id).enqueue(object :
        Callback<MovieResponse> {
        override fun onResponse(call: Call<MovieResponse>, response: Response<MovieResponse>) {
            if(response.code() == 200 ) {
                val items = MC(g[index].id,g[index].name,response.body().results)
                index++
                dataMovie.add(items)
                if((index) < g.count()){
                    getMovie()
                }else {
                    listMovieCategorys.postValue(dataMovie)
                }
            }

            Log.d("RESPONSE",response.raw().request().url().toString())
        }
        override fun onFailure(call: Call<MovieResponse>, t: Throwable){
            Log.e("Ops",t.message)
        }
    })
}

效果不错

我的问题是当我滚动 recyclerview 时,某些图像将被重新加载并显示带有电影标题的错误图像。我搜索并且有些人建议使用 picaso 或 glide,所以我像这样使用 glide

override fun onBindViewHolder(cardViewViewHolder: CardViewViewHolder, i: Int) {
    val movie = listMovie[i]
    try {
        if (movie.poster_path != null && movie.poster_path != ""){
            val options = RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.ic_launcher_round)
                .error(R.mipmap.ic_launcher_round)

            Glide.with(context!!).load("https://image.tmdb.org/t/p/w342/"+movie.poster_path).apply(options).into(cardViewViewHolder.poster)
        }
    }  catch (e: Exception) {
        Log.d("Poster", e.message.toString())
    }
    cardViewViewHolder.title.text = movie.title
    cardViewViewHolder.itemView.setOnClickListener {
        if(context != null){
            val moveWithDataIntent = Intent(context, DetailItem::class.java)
            moveWithDataIntent.putExtra(DetailItem.EXTRA_CLASS, "MOVIE")
            moveWithDataIntent.putExtra(DetailItem.EXTRA_DATA, listMovie[i])
            context!!.startActivity(moveWithDataIntent)
        } else {
            context = it.getContext()
        }

    }

}

但仍然没有解决我的问题

nested recyclerview problem image when scroll

更新(问题是在 GLIDE 中加载 NULL 上下文)

在条件中添加一些更改以检查海报后,我发现在加载图像时出现问题,问题在上下文中,在滚动上下文中,我现在不知道为什么,而是更改了 cardViewViewHolder itslef 和更具体的上下文到我的海报组件

try {
        if (movie.poster_path != null && movie.poster_path != ""){
            val options = RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.ic_launcher_round)
                .error(R.mipmap.ic_launcher_round)

            Glide.with(cardViewViewHolder.poster.getContext()).load("https://image.tmdb.org/t/p/w342/"+movie.poster_path).apply(options).into(cardViewViewHolder.poster)
        } else {
            Log.d("PosterLog1", movie.poster_path.toString())
            cardViewViewHolder.poster.setImageResource(R.mipmap.ic_launcher_round)
        }
    }  catch (e: Exception) {
        Log.d("PosterLog2", e.message.toString())
        cardViewViewHolder.poster.setImageResource(R.mipmap.ic_launcher_round)
    }

我没有注意到,因为图像在第一次运行时加载正确

【问题讨论】:

    标签: android kotlin android-recyclerview android-glide


    【解决方案1】:

    在不显示图像时添加占位符图像,例如在 else 条件和 catch 块中。

    try {
            if (movie.poster_path != null && movie.poster_path != ""){
                val options = RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher_round)
                    .error(R.mipmap.ic_launcher_round)
    
                Glide.with(context!!).load("https://image.tmdb.org/t/p/w342/"+movie.poster_path).apply(options).into(cardViewViewHolder.poster)
            }else{
            cardViewViewHolder.poster.setImageResource(R.mipmap.ic_launcher_round) //placeholder image
            }
        }  catch (e: Exception) {
            Log.d("Poster", e.message.toString())
            cardViewViewHolder.poster.setImageResource(R.mipmap.ic_launcher_round) //placeholder image
        }
    

    希望这会有所帮助!

    【讨论】:

    • 好吧,我明白了,滚动后我得到了所有海报的占位符。我用滑行检查加载图像中的问题
    • 我注意到问题是在滑行时加载空上下文
    • 感谢您的回复
    猜你喜欢
    • 2016-09-15
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多