Android ListView选中所有复选框
我有方法在listView中选择所有复选框,有时复选框不起作用。 它发生在我滚动到最后一项并返回时 此方法调用活动:Android ListView选中所有复选框,android,listview,checkbox,android-adapter,Android,Listview,Checkbox,Android Adapter,我有方法在listView中选择所有复选框,有时复选框不起作用。 它发生在我滚动到最后一项并返回时 此方法调用活动: public void setAllBoxesChecked(boolean value) { for(int i=0; i < lvSklepy.getCount(); i++) { View view = lvSklepy.getAdapter().getView(i, null, lvSklepy);
public void setAllBoxesChecked(boolean value)
{
for(int i=0; i < lvSklepy.getCount(); i++)
{
View view = lvSklepy.getAdapter().getView(i, null, lvSklepy);
CheckBox cb = (CheckBox) view.findViewById(R.id.cb_sklep);
cb.setChecked(value);
}
}
在具有列表视图布局的XML文件中添加以下代码
android:descendantFocusability="blocksDescendants"
假设您有一个带有文本视图的相对布局-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/look_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/>
<CheckBox
android:id="@+id/look_checkbox"
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
/>
您需要将选中的值保存在一个列表中,并在getView方法中从中读取。我使用“技巧”将私有布尔状态添加到适配器中。在“活动”中,添加新项并从arraylist(适配器)中删除最后一项,然后调用notifyDataSetChanged。适配器已刷新,checbox将从状态设置值
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/look_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/>
<CheckBox
android:id="@+id/look_checkbox"
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
/>