1

所以我做到了这一点,请记住,listView 是动态的,因为它是从 API 调用的。

  • ListView 的适配器。
  • 上下文操作栏实现。

现在需要什么。

  • 使 Contextual ActionBar 和 CheckBox 项目同步(例如,如果所有图标都未选中,则操作栏将被销毁)

我尝试了什么:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder vh;

            if(convertView == null){
                vh = new ViewHolder();
                convertView = mInflater.inflate(R.layout.projectlist_frame, null);
                vh.projectTitle = (TextView)convertView.findViewById(R.id.projecttitle);
                vh.projectSector = (TextView)convertView.findViewById(R.id.projectsector);
                vh.cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
                convertView.setTag(vh);

            } else{
                vh = (ViewHolder)convertView.getTag();
            }
            vh.projectTitle.setText(mData.get(position).get("title").toString());
            vh.projectSector.setText(mData.get(position).get("sector").toString());


            vh.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub

                    if(isChecked){
                            if(mMode==null){
                                mMode = startActionMode(mActionCallBack);
                            }
                    }
                    else{
                        if(mMode!=null){
                            mMode.finish();
                        }
                    }
                }
            });

            return convertView;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return super.getViewTypeCount();
        }

        class ViewHolder{
            TextView projectTitle, projectSector;
            CheckBox cb;
        }
    }

For in Contextuals ActionBar

private ActionMode.Callback mActionCallBack = new ActionMode.Callback() {

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub
            for (int i = 0; i < list.getAdapter().getCount(); i++)
                list.setItemChecked(i, false);

            if (mode == mMode) {
                mMode = null;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            mode.setTitle("Options");
            mode.getMenuInflater().inflate(R.menu.contextual_menu_add_project, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // TODO Auto-generated method stub
            long[] selected = list.getCheckedItemIds();
            if (selected.length > 0) {
                for (long id: selected) {
                    // Do something with the selected item
                }
            }
            mode.finish();
            return false;
        }
    };

有人可以帮我跟踪 onCheck 事件并将其操作与上下文 actionBar 协调。

4

0 回答 0