gpt4 book ai didi

android - CheckBox.setChecked() 正在执行但未显示在屏幕上

转载 作者:行者123 更新时间:2023-11-29 01:01:51 24 4
gpt4 key购买 nike

我在 TextView 上使用 onClickListener 来设置复选框上的 setCheck() 并保存一个值,同时清除 itemStateArray 中的最后一个检查项目,这是一个 SparseBooleanArray。代码正在执行,但未显示选中的复选框。

textView.setOnClickListener(v -> {
int adapterPosition = getAdapterPosition();
if (!itemStateArray.get(adapterPosition, false)) {
if (first) {
checkBox.setChecked(true);
first = false;
lastChecked = checkBox;
} else {
if (checkBox.isChecked() && lastChecked != null) {
lastChecked.setChecked(false);
checkBox.setChecked(true);
lastChecked = checkBox;
}
}
itemStateArray.clear();
itemStateArray.put(adapterPosition, true);

} else {
checkBox.setChecked(false);
itemStateArray.clear();
itemStateArray.put(adapterPosition, false);

}
});

我在 textview 上设置监听器,因为我想点击与屏幕侧面较小的复选框相比较大的 textview。

单击该项目并向下和向上滚动后,该项目显示为已选中。我在 onBindViewHolder 中将复选框设置为已选中使用以下代码:

holder.bind(position);

我要将选中位置传递给的绑定(bind)函数:

void bind(int position) {
if (!itemStateArray.get(position, false)) {
checkBox.setChecked(false);
} else {
checkBox.setChecked(true);
lastChecked = checkBox;
}
}

动画中的复选框不会显示在屏幕上。

最佳答案

您正在使用 listView/RecyclerView 对吗?我认为实现此目的的最佳方法是告诉 View 使用方法刷新该行

recyclerView.notifyItemChanged(int)

其中 int 是复选框所在行的位置。

关于android - CheckBox.setChecked() 正在执行但未显示在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50491671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com