android - 选中和取消选中 ListView 中的所有复选框

标签 android android-listview

令我惊讶的是,我无法在 Stack 上找到可用于此目的的现有答案,所以我来了。

我有一个 ListFragment,它有一个附加到 SimpleCursorAdapter 的列表,该列表由以下 row.xml 文件定义的行组成:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:padding="6dip" >

   <CheckBox
       android:id="@+id/story_check_box"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:focusable="false"
       android:focusableInTouchMode="false" />

   <TextView
       android:id="@+id/story"
       android:layout_width="wrap_content"
       android:layout_height="24sp"
       android:lines="1"
       android:scrollHorizontally="true"
       android:singleLine="true"
       android:layout_alignBaseline="@+id/story_check_box"
       android:layout_alignBottom="@+id/story_check_box"
       android:layout_toRightOf="@+id/story_check_box" />

</RelativeLayout>

我在 ListFragment 中使用以下代码将列表与适配器连接起来:

adapter = new SimpleCursorAdapter(getActivity(), R.layout.row, null, new String[] { CProvider.Stories.TITLE }, new int[] { R.id.story }, 0);
setListAdapter(adapter);

然后我尝试在 fragment 中使用 CheckBox 来切换所有列表复选框,如下所示:

CheckBox selectAll = (CheckBox) rootView.findViewById(R.id.select_check_box);
    selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            final ListView listView = getListView();
            for(int i=0; i < getListAdapter().getCount(); i++){
                View view = getViewByPosition(i, listView);
                CheckBox cb = (CheckBox)view.findViewById(R.id.story_check_box);
                if (isChecked) {
                    cb.setChecked(true);
                }
                else {
                    cb.setChecked(false);
                }
            }
        }

    });

我从这里得到了 getViewByPosition:Get ListView children that are not in view ,并且几乎 有效,但是一些复选框没有被选中(并且有一个模式,但我似乎无法弄清楚)。它似乎也比我认为必要的要笨拙一些。

我想要左边的复选框,所以我不想使用 checkedtextviews。也许我需要扩展 CursorAdapter 并覆盖 getView?

提前致谢。

最佳答案

也许我没有正确理解您的问题,但我的理解是,由于一个“全选复选框”,您想选中和取消选中所有复选框。

然后,我要做的是将“全选复选框”的状态作为类的变量(作为 bool 值),它会被您的 selectAll.setOnCheckedChangeListener 覆盖,然后对适配器说“嘿,我的状态变了!”每次复选框改变其状态。 像这样:

class Dummy{
    boolean isAllSelected = false;
    Checkbox selectAll = (find or create your CheckBox)
    selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) isAllSelected = true;
            else isAllSelected = false;
            listView.getAdapter().notifyDataSetChanged();
        }
    }

然后,您只需重写此适配器的 getView()(如您建议的那样)并添加“if (isAllSlected)”条件。

对我来说,这听起来最简单,但每次用户单击复选框时都调用 notifyDataSetChanged() 方法可能不太好(对于如此小的更改效率不高)。无论如何,希望它能有所帮助(我写的代码可能语法不正确:我直接在网站表单上写的)!

关于android - 选中和取消选中 ListView 中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026347/

相关文章:

android - ViewPager 与 fragment 内的 ListView - 修改时 ListView 不会更新

Android 自定义轮盘或老虎机实现

android - 关于Android设备HTML5网页编码的两个问题

java - 在 ListView 中限制项目

android - 我应该在 ListView 中膨胀布局吗?

java - Android:如何更改 ProgressBar 的高度?

Android:在 ListAdapter 中设置 TextView 的可见性

java - Netbeans 看不到引用库中的类

Java - 在 Android 应用程序上打印非拉丁字符

php - 从数据库中选择 php 脚本中的 id (Android)