选择多个子onlongpress表单expandablelistview android

时间:2016-12-16 09:30:36

标签: android contextmenu

//这是我的适配器类。我希望按下长按子视图选择多个子项并打开一个上下文工具栏,我可以通过它执行任何操作。对于listview它可以正常工作但是在expandable listview我无法做到。

public class ExpandListAdapter extends BaseExpandableListAdapter
    {
        private Context context;
        LayoutInflater inflater;
        private SparseBooleanArray mSelectedItemsIds;

        public ExpandListAdapter(Context context, ArrayList<Parent_date_bill> mparent)
        {
            this.context = context;
            mSelectedItemsIds=new SparseBooleanArray();

        }
        @Override
        public int getGroupCount()
        {
            Log.d("parent size", "" + parent.size());
            groupcount = parent.size();
            return parent.size();
        }
        @Override
        public int getChildrenCount(int groupPosition) {
            Log.d("child count", "" + groupPosition);

            child = parent.get(groupPosition).getBills();
            Log.d("child count", "" + child.size());
            return child.size();
        }
        @Override
        public Object getGroup(int groupPosition) {

            return parent.get(groupPosition);
        }
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            child = parent.get(groupPosition).getBills();
            // Log.d("get child ",""+child.get(childPosition));
            return child.get(childPosition);
        }
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            Parent_date_bill group = (Parent_date_bill) getGroup(groupPosition);

            if (convertView == null)
            {
                LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

                convertView = inf.inflate(R.layout.sectionheader_bill, null);
                holder1 = new ViewHolder1();
                holder1.bildate = (TextView) convertView.findViewById(R.id.txtbill_heading);             
                convertView.setTag(holder1);
            }
            else
            {
                holder1 = (ViewHolder1) convertView.getTag();

            }
            Log.d("groupdate=", "group&&" + group.getDate().toString());

            holder1.bildate.setText("" + group.getDate().toString());
        return convertView;
        }
        @Override
        public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            Bills_model child = (Bills_model) getChild(groupPosition, childPosition);
            ViewHolder holder = new ViewHolder();
            if (convertView == null)
            {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.singlerow_billdetail, null);
                holder.billamount = (TextView) convertView.findViewById(R.id.txtbillamount);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.billamount.setText(getResources().getString(R.string.Rs) + " " + child.getTotal_amnt_afterdiscount() + " ");

            return convertView;

        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) 
    {
            return true;
        }

        public SparseBooleanArray getSelectedIds()
        {
            return mSelectedItemsIds;
        }

        public void toggleSelection(int position)
        {
            selectView(position, !mSelectedItemsIds.get(position));
        }

        public void removeSelection()
        {
            mSelectedItemsIds = new SparseBooleanArray();
            notifyDataSetChanged();
        }

        public void selectView(int position, boolean value)
        {
            if (value)
                mSelectedItemsIds.put(position, value);
            else
                mSelectedItemsIds.delete(position);

            notifyDataSetChanged();
        }


    }




// and this is my expandablelistview setMultiChoiceModeListener

      listView_bill.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
                @Override
                public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {

                }

                @Override
                public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                    return true;
                }

                @Override
                public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                    return true;
                }

                @Override
                public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                    return true;
                }

                @Override
                public void onDestroyActionMode(ActionMode actionMode) {

                }
            });

0 个答案:

没有答案