【问题标题】:Android Kotlin expanding textview in adapter causes unwanted expanding another textviewsAndroid Kotlin 在适配器中扩展文本视图会导致不必要的扩展另一个文本视图
【发布时间】:2022-07-03 04:42:01
【问题描述】:

我使用适配器和 recyclerview 显示项目列表。每个项目仅显示 4 行 bio-textView。为了查看整个传记,用户单击“展开”textView。我的代码可以运行,但存在一个问题,因为有时(以某种方式随机)一次“展开”点击(例如在第 1 项中)会导致在另一个项目中展开 2-3 传记 textView 更多(例如第 7 和第 12 项)。我的代码有什么问题?如何检查它/确定为什么项目 textView 是额外扩展的? 有人可以帮忙吗?

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)
    }
}

}

【问题讨论】:

    标签: android kotlin android-recyclerview expand


    【解决方案1】:

    终于搞定了。 我在 RecyclerView 适配器代码中添加了如下,一切正常。没有翻倍的物品或意外的行为。但它为什么有效?有人知道吗?

    override fun getItemViewType(position: Int): Int {
            return position
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多