如何使用切换按钮启用或禁用列表视图中的所有复选框 android

How to enable or disable all checkbox on List view with toggle button android

大家好,我需要有关使用工具按钮启用或禁用列表视图项目的帮助。

在我的例子中,有一个带有列表 item_namecheckobxs 的自定义列表视图。

这是我使用 listview ebale 或禁用但不起作用的复选框代码。

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.create_campagin_lists, container, false);


        mydbhelper = new DBhelper(getContext());

        ContactsTypes = mydbhelper.getCaontactsTypesData();

        CheckBox03 = (CheckBox) view.findViewById(R.id.CheckBox03);

        listView = (ListView) view.findViewById(R.id.campaign_list_view);

        dataAdapter = new MyCustomAdapter(getContext(), R.layout.layout_contact_row, ContactsTypes);
        listView.setAdapter(dataAdapter);

        CheckBox03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox03.setSelected(CheckBox03.isChecked());
                if (CheckBox03.isChecked()) 
                {
                    listView.setEnabled(false);
                }
           else {
                    listView.setEnabled(true);
                }
                                       }
                                                            });
        return view;
    }

这是我的适配器

 private class MyCustomAdapter extends ArrayAdapter<Contacts> {
        private ArrayList<Contacts> contactsList;
        public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<Contacts> countryList) {
            super(context, textViewResourceId, countryList);
            this.contactsList = new ArrayList<Contacts>();
            this.contactsList.addAll(countryList);
        }
        private class ViewHolder {
            RelativeLayout layout;
            TextView name;
            TextView count;
            CheckBox id;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.fragmentsrow, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.listview_item_title);
                holder.count = (TextView) convertView.findViewById(R.id.listview_item_count);
                holder.id = (CheckBox) convertView.findViewById(R.id.CheckBox01);
                holder.layout = (RelativeLayout) convertView.findViewById(R.id.android_list_view_tutorial_with_example);
                convertView.setTag(holder);
              } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Contacts contact = contactsList.get(position);
            String name = contact.getName();
            String code = contact.getCode();
            boolean status = contact.isSelected();
                holder.name.setText(name);
                holder.count.setText(code);
                holder.id.setChecked(status);
                holder.id.setTag(contact);

            return convertView;
        }

和列表视图上方的工具按钮。

我需要 禁用 当 toogle 按钮 ON.

时 Listview 中可用的所有复选框

与切换按钮 关闭 然后 启用 列表视图中的所有复选框相同。

使用以下说明.........

Create Static boolean variable inside your Activity Class.

Attached that boolean with your Toggle button means set onclick listener on toggle and change boolean variable.

And use this boolean variable to enable and disable your Checkboxes.

试试这个代码....

public static boolean flagToggle=false;//declare globally in Fragment....

使用此代码代替您的 CLICK Listener 更改 flagToggle 值...... OnCheckedChangeListener 用于识别复选框布尔值(表示为您提供复选框是否被选中的值)

CheckBox03.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
          flagToggle=isChecked;
          dataAdapter.notifyDataSetChanged;
       }
   }
); 

并在您的适配器内部使用 getView() 方法此代码.........

if(FragmentName.flagToggle){
//here set your Views Enable
}else{
//here set your Views Disable
}

随时询问您是否遇到任何问题

享受编码...

在你的 Activity class:

CheckBox03.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                dataAdapter.setToggleStatus(b);
                dataAdapter.notifyDataSetChanged;
            }
        });

然后在你的适配器class中,先添加一个全局变量

private boolean toggleState = true; //make sure its true when you initialize it

然后在adapterclass本身添加这个方法

public void setToggleStatus(boolean status){
   toggleState = status
}

然后在 getView() 方法中 return convertView; 之前添加这一行

holder.id.setClickable(toggleState);
or
holder.id.setEnabled(toggleState);