如何更新Recyclerview适配器上仅单击的项目而不是最后一个可见的项目?

时间:2019-05-29 05:10:29

标签: android recycler-adapter

我之前曾问过this个问题,经过一番调查后,我知道onBindViewHolder更新了我的列表中的错误项目。这是什么意思?例如,我按了列表中的0项目,它将更新第6个项目的视图。我不知道为什么会这样,因为我以前认为notifyItemChanged()会更改单击的项目而不是列表中最后一个可见的项目。我的适配器类:

open class JobsAdapter(private val jobs: ArrayList<JobModel>, private val ctx: Context, private val click: Click) : RecyclerView.Adapter<JobsAdapter.JobsHolder>() {
    lateinit var view: View


    override fun onCreateViewHolder(viewGroup: ViewGroup, p1: Int): JobsHolder {
        view = LayoutInflater.from(viewGroup.context).inflate(R.layout.job_list_item, viewGroup, false)
        return JobsHolder(view)
    }

    override fun getItemCount(): Int {
        return jobs.size
    }


    override fun onBindViewHolder(holder: JobsHolder, position: Int) {
        val jobModel = jobs[position]
        holder.tvPosition.text = jobModel.position
        holder.tvAddress.text = jobModel.location
        holder.tvCompany.text = jobModel.company_name

        holder.itemView.setOnClickListener {
            if (!jobModel.isSeen) {
                    Singleton.clickReq(jobModel.id, ctx)

            }

            holder.itemView.background = ContextCompat.getDrawable(ctx, R.drawable.message_list_item_bg)

            click.sendData(Uri.parse(jobModel.job_link).toString(), holder.adapterPosition, jobModel.position!!)
        }

        holder.ivAddTo.setOnClickListener {
            holder.progressBar.visibility = View.VISIBLE
            holder.ivAddTo.setBackgroundResource(R.drawable.added_to_notepad)
            holder.ivAddTo.visibility = View.GONE
            holder.ivAddTo.isClickable = false
            Singleton.jobLstPos = position



                saveToNotepad(jobModel.id)
        }


        if (jobModel.isSeen) {
            holder.itemView.background = ContextCompat.getDrawable(ctx, R.drawable.message_list_item_bg)
        } else {
            holder.itemView.setBackgroundColor(Color.parseColor("#fafafa"))
        }
    }

    override fun onBindViewHolder(holder: JobsHolder, position: Int, payloads: List<Any>) {
        if (payloads.isNotEmpty()) {
            when (payloads[0]) {
                1 -> {
                    holder.ivAddTo.visibility = View.GONE
                }
            }
        } else {
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    private fun saveToNotepad(id: Int) {
        Singleton.apiService(ctx).saveTo("Bearer " + ctx.getSharedPreferences("appD", 0).getString("access_token", ""), id).enqueue(object : Callback<SaveTo> {
            override fun onResponse(call: Call<SaveTo>, response: Response<SaveTo>) {
                if (response.isSuccessful) {
                    notifyItemChanged(view.getTag(R.id.tag_3) as Int, 1)
                } 
            }

            override fun onFailure(call: Call<SaveTo>, t: Throwable) {
            }
        })
    }

    class JobsHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvCompany = itemView.findViewById<TextView>(R.id.tv_company)!!
        var tvDate = itemView.findViewById<TextView>(R.id.tv_date)!!
        var tvPosition = itemView.findViewById<TextView>(R.id.tv_job_title)!!
        var tvAddress = itemView.findViewById<TextView>(R.id.tv_address)!!
        var ivAddTo = itemView.findViewById<ImageView>(R.id.add_to_notepad)!!
        var progressBar = itemView.findViewById<ProgressBar>(R.id.progressBar)
    }
}

单击添加按钮后,我发送请求,如果成功,我想停止进度条,更改其可见性并更改单击的btn的背景。那么,为什么会发生这种情况以及如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

我设法通过添加特殊功能来更新视图来解决我的问题:

fun changeView(position: Int, permission: Boolean) {
        notifyItemChanged(position, permission)
    }

然后是onBindViewHolder()

override fun onBindViewHolder(holder: JobsHolder, position: Int, payloads: List<Any>) {
        if (payloads.isNotEmpty()) {
            when (payloads[0]) {
                true -> {
                    holder.ivAddTo.setBackgroundResource(R.drawable.added_to_notepad)
                    holder.progressBar.visibility = View.GONE
                    holder.ivAddTo.visibility = View.VISIBLE
                }
                false->{
                    holder.progressBar.visibility = View.GONE
                    holder.ivAddTo.visibility = View.VISIBLE
                }
            }
        } else {
            super.onBindViewHolder(holder, position, payloads)
        }
    }

也许会帮助其他人。