I am using recyclerview it has 22 items in the list but whenever I select any item for downward or upward it will gone and a previously selected one appears because it recycled that view.
fun setData(dataList: ArrayList<Playerv2>?) { this.dataList = dataList }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view: View
val mInflater = LayoutInflater.from(context)
view = mInflater.inflate(R.layout.adapter_favourite_playerv2, parent, false)
return FavouritePlayerViewHolder(view)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as FavouritePlayerViewHolder).tvFavPlayerId.text = dataList?.get(position)?.shirtnumber.toString()
holder.tvPlayerFirstName.text = dataList?.get(position)?.firstname + " " + dataList?.get(position)?.lastname
holder.checkBox.text = dataList?.get(position)?.firstname + dataList?.get(position)?.lastname
if (holder.checkBox.text.toString().equals(previouslySelectedOption, ignoreCase = true)) {
holder.checkBox.isChecked = true
lastCheckedCheckbox = holder.checkBox
dataSelectedHolder(holder)
} else {
holder.checkBox.isChecked =
false
dataUnselectedHolder(holder)
}
holder.checkBox.text = ""
holder.checkBox.setOnCheckedChangeListener { compoundButton, b ->
if (lastCheckedCheckbox != null) {
lastCheckedCheckbox?.isChecked = false
lastCheckedCheckbox = null
dataUnselectedHolder(holder)
}
if (holder.checkBox.isChecked) {
lastCheckedCheckbox =
holder.checkBox
dataSelectedHolder(holder)
}
onRecyclerItemClicked.onItemClicked(dataList?.get(position)?.shirtnumber + ":" + dataList?.get(position)?.firstname + " " + dataList?.get(position)?.lastname)
}
}
fun dataSelectedHolder(holder: FavouritePlayerViewHolder) {
holder.tvPlayerFirstName.setTextColor(Color.RED)
holder.tvFavPlayerId.setTextColor(Color.RED)
holder.ConstraintbackgroundColor.setBackgroundResource(R.color.white)
}
fun dataUnselectedHolder(holder: FavouritePlayerViewHolder) {
holder.tvFavPlayerId.setTextColor(Color.WHITE)
holder.tvPlayerFirstName.setTextColor(Color.WHITE)
holder.ConstraintbackgroundColor.setBackgroundResource(R.color.background_profile_color_0a2240)
//Set imageView Here as well.
}
override fun getItemCount(): Int {
return dataList!!.size
}
class FavouritePlayerViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var checkBox: CheckBox
// ImageView ivPlayerImage;
var tvFavPlayerId: TextView
var tvPlayerFirstName: TextView
var tvFavPlayerLastName: TextView? = null
var ConstraintbackgroundColor: ConstraintLayout
init {
checkBox = itemView.findViewById(R.id.cbFavPlayer)
// ivPlayerImage = itemView.findViewById(R.id.ivPlayerImage);
tvFavPlayerId = itemView.findViewById(R.id.tvFavPlayerId)
tvPlayerFirstName = itemView.findViewById(R.id.tvFavPlayerFirstName)
ConstraintbackgroundColor = itemView.findViewById(R.id.ConstraintbackgroundColor)
}
}
}
For Example, Previously I have selected the first option then I will select the last one in the list then I scrolled up and I see previously one is selected
Aucun commentaire:
Enregistrer un commentaire