gpt4 book ai didi

android - ExpandableListView 子项目的 CheckBox 被随机检查

转载 作者:太空狗 更新时间:2023-10-29 12:45:41 26 4
gpt4 key购买 nike

我创建了一个带有相当复杂的子项 xml 的 ExpandableListView。它包含一个 TextView、一个复选框和一个 EditText。这是 CheckBox 部分:

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal"
android:stretchColumns="0">
<TableRow>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/lblListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"/>
<CheckBox
android:id="@+id/cboxIsRel"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

</CheckBox>
</TableRow>
</TableLayout>

如果我检查第一项,则在每组中也会检查第 5 项(如果选择了第 2 项,则选择第 6 项)。

为什么他们也被选中了?

更新:

getChildView 方法中我有这段代码:

if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);

如果我不使用 convertView 检查,则不会重复使用该复选框,但如果我向外滚动,则选中的复选框也会取消选中。

最佳答案

这是因为 View 的回收。

确保在您的适配器上,bindView() 或 getView() 方法将复选框设置为选中或未选中。

编辑:

ListView 重用 View 以提高性能,因此在您的情况下,第一个项目被检查,然后在第 5 个子项上使用相同的 View ,因此它也被检查的原因。

因此,为了防止这种情况发生,您必须保存选中的位置,并且在您的 getChildView() 方法中,您应该根据保存的值选中/取消选中您的复选框。

这样,您检查了第一个项目,然后滚动并且 View 被回收到第 5 个子项,适配器将调用 getChildView 获取位置 5,您将检查它是否被选中。由于未选中,您在复选框上调用 setchecked(false),它将显示为未选中。

关于android - ExpandableListView 子项目的 CheckBox 被随机检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18637554/

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