I am trying to set a background color for 1 item in my recyclerview when I click on a button. I get the index of the particular item and try to use a function that includes notifyItemChange in the onclick method of my button in Activity. But it keeps changing the color of the first item in the recyclerview even if I hard code an index number into the function. I know that I am getting the correct index of the selected item, but I'm missing something. Perhaps some code in the adapter? I cannot glean enough information from other answers to figure out how this should be done. Any help? Thanks!
Variable for selectedPosition in Activity:
private var newSelectedPosition: Int = 0
Button onClick in Activity:
R.id.btn_add_daily_report_submit -> {
Log.e("tag", "$newSelectedPosition")
updateSingleItem(newSelectedPosition)
}
Function updateSingleItem() in Activity:
private fun updateSingleItem(index: Int) {
Log.e("updateSingleItem", "$newSelectedPosition")
card_view.setBackgroundColor(resources.getColor(R.color.green))
adapter.notifyItemChanged(index)
}
setOnClickListener from adapter interface to record the selected item's index in Activity:
adapter.setOnClickListener(object :
DailyReportsAdapter.OnClickListener {
override fun onClick(position: Int, user: User) {
newSelectedPosition = position
Log.e("tag", "$newSelectedPosition")
et_daily_report_employee.text = getString(
R.string.tv_name,
user.firstName,
user.lastName
)
}
})
Adapter class:
open class DailyReportsAdapter(
private val context: Context,
private var list: ArrayList<User>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// A global variable for OnClickListener interface.
private var onClickListener: OnClickListener? = null
private var selectedItemPosition: Int = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return MyViewHolder(
LayoutInflater.from(context).inflate(
R.layout.item_daily_report_layout,
parent,
false
)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, @SuppressLint("RecyclerView") position: Int) {
val model = list[position]
if (holder is MyViewHolder) {
holder.itemView.employee_name.text = "${model.firstName} ${model.lastName}"
j
// Assign the on click event for item view and pass the required params in the on click function. holder.itemView.setOnClickListener { if (onClickListener != null) { onClickListener!!.onClick(position, model) } } } }
override fun getItemCount(): Int {
return list.size
}
fun setOnClickListener(onClickListener: OnClickListener) {
this.onClickListener = onClickListener
}
/**
* An interface for onclick items.
*/
interface OnClickListener {
fun onClick(position: Int, user: User)
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)
}
Here is my solution. I still need to persist the selected items with sharedPrefs, but it works as is! Thanks @cactustictacs
My Adapter class:
}
And in my Activity I used the following code:
Global variable:
Button Click in Activity:
ItemClick from interface in adapter where I record the position of the selected item. This is also in Activity: