Gmail-like ListView with checkboxes (and using the ActionBar)

后端 未结 3 1995
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 13:07

I\'m trying to recreate what Google did with the ListView in the Gmail app. In particular, I would like to have each list item include a CheckBox and two TextViews (one on t

3条回答
  •  隐瞒了意图╮
    2020-12-04 13:27

    The mode in the figure is called action mode. If you are using your custom row view and custom adapter, you don't have to use CHOICE_MODE_MULTIPLE_MODAL to start action mode. I tried it once and failed, and I suspect it is used for built-in adapter.

    In order to call the action mode by yourself in your code, call method startActionMode in any listener method of any of your view, let it be checkbox, textview or something like that. You then pass the ModeCallback as parameters into it, and do whatever operation you want to do in ModeCallback class.

    I don't think this one would work on pre-3.0 Android though.

    Here is a brief example. I have a expandable list activity and would like to call out the action mode menu when user check/uncheck the checkbox, and here is my getChildView method in my custom adapter class:

    public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
    
            if (convertView == null) {
                LayoutInflater inflater =  (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.childrow, null);
            }
            CheckBox cb = (CheckBox)convertView.findViewById(R.id.row_check);
            cb.setChecked(false);   // Initialize
            cb.setTag(groupPosition + "," + childPosition);
            cb.setFocusable(false); // To make the whole row selectable
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String tag = (String)buttonView.getTag();
                    String[] pos = tag.split(",");
                    if (isChecked) {
                        if (mActionMode == null) mActionMode = startActionMode(mMultipleCallback);                      
                    }
                    else {
                        if (mActionMode != null) {  // Only operate when mActionMode is available
    
                            mActionMode.finish();
                            mActionMode = null;
                        }
                    }
                }
            });
    
            TextView tvTop = (TextView)convertView.findViewById(R.id.row_text_top);
            TextView tvBottom = (TextView)convertView.findViewById(R.id.row_text_bottom);
            tvTop.setText(mChildren.get(groupPosition).get(childPosition));
            tvBottom.setText(mChildrenSize.get(groupPosition).get(childPosition));
            return convertView;
        }
    

提交回复
热议问题