'Recyclerview recreating dynamic chips on scroll
I am creating a RecyclerView
which has an item with dynamic chips. I am looping through an array to set the text of the chip, this works fine but when I scroll the RecyclerView
, more chips gets created
class Adapter(activity: Activity): RecyclerView.Adapter<Adapter.Holder>() {
val mActivity = activity
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val itemView: View?
itemView = parent.inflate(R.layout.item, false)
return Holder(itemView)
}
override fun getItemCount(): Int {
return 7
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.bindView()
}
inner class Holder(view: View): RecyclerView.ViewHolder(view){
private val binding: ItemBinding = ItemBinding.bind(view)
private val array: ArrayList<String> = ArrayList(5)
init {
}
fun bindView() {
binding.textView.text = ""
for (item in array){
val mChip: Chip = mActivity.layoutInflater.inflate(R.layout.item_chip,null,false) as Chip
mChip.text = item
val paddingDp = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10f,
mActivity.resources.displayMetrics
).toInt()
mChip.setPadding(paddingDp, 0, paddingDp, 0)
mChip.setOnCloseIconClickListener { Toast.makeText(mActivity.applicationContext, absoluteAdapterPosition.toString()+ mChip.text,Toast.LENGTH_SHORT).show() }
binding.chips.addView(mChip)
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|