滚动后嵌套 Recyclerview 中的图像更改为错误图像

image change to wrong image in nested recyclerview after scroll

我用 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 中加载空上下文)

在检查海报的条件中添加一些更改后,我发现使用滑行加载图像时出现问题,问题出在上下文中,滚动时上下文变为空,我现在不知道为什么,并且更改了 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)
    }

我没有注意到,因为图像首先正确加载 运行

在不显示图像时添加占位符图像,例如 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
    }

希望这会有所帮助!!