'Android Kotlin expanding textview in adapter causes unwanted expanding another textviews

I show a list of items using adapter and recyclerview. Each item shows only 4 lines of biography-textView. In order to see whole biography, user clicks 'expand' textView. My code functions but there is a problem because sometimes (somehow randomly) one 'expand' click (f.e. in item No 1) causes expanding 2-3 biography textView more in another items(f.e. items No 7 and 12). What is wrong with my code? How to check it/identify why items textView are extra expand? Can anyone help?

class ShowMastersAdapter (
    private val masterList: ArrayList<Master>,
    private val itemListener: OnItemClickListener
) : RecyclerView.Adapter<ShowMastersAdapter.ShowMasterViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShowMasterViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_master, parent, false)
        return ShowMasterViewHolder(view)
    }

    override fun onBindViewHolder(holder: ShowMasterViewHolder, position: Int) {
        holder.bindData(position)
    }

    override fun getItemCount(): Int {
        return masterList.size
    }
    inner class ShowMasterViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        fun bindData(position: Int) {

            val masterBiography = itemView.findViewById<TextView>(R.id.item_master_biography)
            val masterExpandBiography =itemView.findViewById<TextView>(R.id.item_master_expand_text)

            masterBiography.text = masterList[position].biography

            masterExpandBiography.setOnClickListener {
                itemListener.onReadMoreTextClick(masterExpandBiography, masterBiography)
            }

        }
    }

    interface OnItemClickListener {
        fun onReadMoreTextClick(expandText: TextView, biographyText: TextView)
    }

}
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/layout_top_part">

    <TextView
        android:id="@+id/item_master_biography"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/item_master_margin_start"
        android:layout_marginEnd="@dimen/item_master_margin_end"
        android:ellipsize="end"
        android:maxLines="4"
        android:text="unknown"
        android:textSize="@dimen/font_main_size_item_master"
        app:layout_constraintBottom_toTopOf="@+id/item_master_expand_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/item_master_biography_label" />

    <TextView
        android:id="@+id/item_master_expand_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="@string/it_master_expand_text"
        android:textColor="@color/text_as_link"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

class FragmentShowMasters() : Fragment(), ShowMastersAdapter.OnItemClickListener{

private var listOfMasters: ArrayList<Master> = ArrayList()
private lateinit var myRecycler: RecyclerView



    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState:Bundle?): View {

    myView = inflater.inflate(R.layout.fragment_show_masters, container, false)
    

    myRecycler = myView.findViewById(R.id.recycler_show_masters)
    myRecycler.setHasFixedSize(true)

    myRecycler.adapter = ShowMastersAdapter(listOfMasters, this, highlightSearchText)
    myRecycler.adapter?.notifyDataSetChanged()

    myRecycler.layoutManager = LinearLayoutManager(this.context)


    return myView
}

override fun onReadMoreTextClick(expandText: TextView, biographyText: TextView) {
    if (biographyText.maxLines == 4) {
        biographyText.maxLines = Int.MAX_VALUE
        expandText.text = getString(R.string.it_master_shrink_text)
    } else {
        biographyText.maxLines = 4
        expandText.text = getString(R.string.it_master_expand_text)
    }
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source