我使用非常复杂的子项xml创建了一个ExpandableListView。它包含TextView,Checkbox和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>
如果我检查第1项,则在每组中也检查第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检查,则不会重复使用复选框,但如果我向外滚动,也会取消选中所选复选框。
答案 0 :(得分:3)
这是因为回收了意见。
确保在适配器上,bindView()或getView()方法将复选框设置为选中或不选。
编辑:
ListView重用视图以提高性能,因此在您的情况下,选中第一项,然后在第五个子项上使用相同的视图,因此也检查它的原因。
因此,为防止这种情况发生,您必须保存选中的位置,并且在getChildView()方法中,您应根据保存的值选中/取消选中复选框。
这样,您检查第一项,然后滚动并将视图循环到第5个子项,适配器将调用getChildView获取位置5,您将检查它是否已被选中。由于未选中,您在复选框上调用setchecked(false),它将显示为未选中状态。