【发布时间】:2021-10-22 01:46:42
【问题描述】:
我需要选择第一个项目作为默认选择并在点击时选择单个项目。但是当我滚动并看到时,我可以看到它在每 8 个项目后被选中。它甚至包括多个项目的 margin-left,我只包括水平 recyclerview 中的第一个项目。
companion object {
private var lastCheckedtab: ConstraintLayout? = null
}
fun bind(
context: Context?,
name: String,
position: Int
) {
if (position == 0) {
cardView?.isSelected = true
cardView?.isClickable = false
lastCheckedtab = cardView
val p = cardView?.layoutParams as ViewGroup.MarginLayoutParams?
p?.leftMargin = 52
}
itemView.setOnClickListener{
val checkedTab = it as ConstraintLayout
checkedTab.isSelected = true
checkedTab.isClickable = false
if (lastCheckedtab != null && lastCheckedtab != checkedTab) {
lastCheckedtab?.isSelected = false
lastCheckedtab?.isClickable = true
context?.let { it1 ->
checkedTab.findViewById<TextView>(R.id.iv_tab_name).setTextColor(
ContextCompat.getColor(
it1,
R.color.black
)
)
lastCheckedtab?.findViewById<TextView>(R.id.iv_tab_name)?.setTextColor(
ContextCompat.getColor(
it1,
R.color.setting_text
)
)
}
}
lastCheckedtab = checkedTab
}
}
override fun onBindViewHolder(holder: MyHolder, position: Int) {
holder.bind(context, tabs.get(position), position)
}
【问题讨论】:
标签: android android-recyclerview horizontalscrollview horizontal-recyclerview