【问题标题】:ListView selecioint all checkboxesListView 选择所有复选框
【发布时间】:2014-04-19 20:08:41
【问题描述】:

我有方法可以选择 listView 中的所有复选框,有时复选框不起作用。 当我滚动到最后一项并返回时会发生这种情况。

此方法调用活动:

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);
        }
    }

适配器: 第一项是选择所有复选框时的复选框。

public View getView(int position,View convertView, final ViewGroup parent) 
    {
....
final CheckBox chboxShop = (CheckBox) convertView.findViewById(R.id.cb_sklep);

        if(position  == 0)
        {

            chboxShop.setOnCheckedChangeListener(new OnCheckedChangeListener() 
            {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    Boolean blnObj = new Boolean(isChecked);

                    callOnClickListener(RES_CHECKBOX, blnObj, chboxShop);   
                }
            });
            rLayout = R.layout.ad_preferowane_sklepy;
        }
        else
        {
            final ImageLayout llImage = (ImageLayout) convertView.findViewById(R.id.il_image);

            chboxShop.setText(item.nazwaPelna);
            llImage.imageFromWeb(item.logoUrl);


        }
...
}

【问题讨论】:

  • 您需要将 Checked 值保存在一个列表中,然后在 getView 方法中读取。
  • 我使用“技巧”将私有布尔状态添加到适配器。在活动中添加新项目并从arraylist(适配器)中删除最后一个项目,然后调用notifyDataSetChanged。适配器已刷新,并且复选框从状态设置值

标签: android listview checkbox android-adapter


【解决方案1】:

在包含您的列表视图布局的 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"

  />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多