【发布时间】:2021-11-20 20:25:48
【问题描述】:
我在 Fragment 中使用 RecyclerView 来显示元素列表,每个元素都包含一个复选框,如下所示。
单击START 按钮后,我想禁用 RecyclerView 中的所有元素。
这是我的代码:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
startButton = view.findViewById<Button>(R.id.start_button)
startButton.setOnClickListener {
// yes, recyclerView is defined
setViewGroupEnabled(recyclerView, false)
}
}
fun setViewGroupEnabled(view: ViewGroup, enabled: Boolean) {
Log.d(TAG, "Numkids: ${view.childCount}")
view.isEnabled = enabled
for (v in view.children) {
if (v is ViewGroup) setViewGroupEnabled(v, enabled)
else v.isEnabled = enabled
}
}
这段代码禁用了recyclerView 中的大部分 元素,但由于某种原因,它跳过了一些,通常一次跳过多个。它似乎也会以一种模式跳过子视图,这种模式会根据我滚动列表的距离而有所不同。
为什么它的行为如此奇怪?
【问题讨论】:
标签: android android-studio android-fragments android-recyclerview