可扩展Listview - 检查组和子中的CheckBox

时间:2016-12-21 07:31:30

标签: checkbox scroll expandablelistview custom-adapter

我正在开发一个可扩展的列表视图,其中包含group和child中的复选框。我的目标是选中组中的复选框,因此也会检查子项中的复选框。但现在我遇到了问题:如果选中了组中的复选框,那么在我看到所有孩子后滚动列表时,将检查孩子..

有人可以帮我解决这个问题吗?提前谢谢。

这是我的适配器:

public class AdapterHistoryExpandable extends BaseExpandableListAdapter {

    private Context context;
    private boolean checkAll = false;
    private ArrayList<GroupItemsInfo> teamName;
    public int isGroupChecked = 0;
    HistoryHeaderActivity main;
    TotalListener mListener;

    class ViewHolder {
        public CheckBox checkChild, checkGroup;
        public TextView date, h_ps_id, h_ps_name, h_dokname, h_item, h_qty, h_service, heading;
    }

    public AdapterHistoryExpandable(HistoryHeaderActivity main, ArrayList<GroupItemsInfo> deptList) {
        this.main = main;
        this.teamName = deptList;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public int getGroupCount() {
        return teamName.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<ChildItemsInfo> productList = teamName.get(groupPosition).getSongName();
        return productList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return teamName.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<ChildItemsInfo> productList = teamName.get(groupPosition).getSongName();
        return productList.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;
    }

    public void setmListener(TotalListener mListener) {
        this.mListener = mListener;
    }

    public void setmGroupList(ArrayList<GroupItemsInfo> mGroupList) {
        this.teamName = mGroupList;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final GroupItemsInfo headerInfo = (GroupItemsInfo) getGroup(groupPosition);
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_items, null);
            holder.checkGroup = (CheckBox) convertView.findViewById(R.id.checkList);
            holder.checkGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!isExpanded)
                        mListener.expandGroupEvent(groupPosition, isExpanded);
                    int getPosition = (Integer) buttonView.getTag();
                    headerInfo.setSelected(isChecked);

                    if (!isChecked) {
                        headerInfo.setSelected(false);
                    }
                    Intent intent = new Intent(context, AdapterHistoryExpandable.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    context.startActivity(intent);
                }
            });
            convertView.setTag(holder);
            convertView.setTag(R.id.checkList, holder.checkGroup);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.heading = (TextView) convertView.findViewById(R.id.heading);
        holder.checkGroup.setTag(groupPosition);
        holder.checkGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                main.groupInfos.get(groupPosition).setSelected(buttonView.isChecked());
            }
        });
        holder.checkGroup.setChecked(main.groupInfos.get(groupPosition).isSelected());

        holder.heading.setText(headerInfo.getName().trim());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildItemsInfo detailInfo = (ChildItemsInfo) getChild(groupPosition, childPosition);
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_history, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.date = (TextView) convertView.findViewById(R.id.date_);
        holder.h_ps_id = (TextView) convertView.findViewById(R.id.patient_id);
        holder.h_ps_name = (TextView) convertView.findViewById(R.id.patient_);
        holder.h_dokname = (TextView) convertView.findViewById(R.id.doctor_);
        holder.h_item = (TextView) convertView.findViewById(R.id.item_);
        holder.h_qty = (TextView) convertView.findViewById(R.id.qty_);
        holder.h_service = (TextView) convertView.findViewById(R.id.servicetype_);
        holder.checkChild = (CheckBox) convertView.findViewById(R.id.checkBox);

        ModelChecking add = new ModelChecking();
        if (main.groupInfos.get(groupPosition).isSelected()) {
            holder.checkChild.setChecked(true);
            if (holder.checkChild.isChecked()) {
                add.setIDItem(detailInfo.getHitem());
                add.setQtyItem(detailInfo.getHqty());
                main.checkingItem.add(add);
            }
        } else {
            holder.checkChild.setChecked(false);
            main.checkingItem.clear();

        }

        holder.h_ps_id.setText(detailInfo.getHp_id().trim());
        holder.date.setText(detailInfo.getDate().trim());
        holder.h_ps_name.setText(detailInfo.getHp_name().trim());
        holder.h_dokname.setText(detailInfo.getHd_name().trim());
        holder.h_item.setText(detailInfo.getHitem().trim());
        holder.h_qty.setText(detailInfo.getHqty().trim());
        holder.h_service.setText(detailInfo.getHservice().trim());

        return convertView;
    }

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

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }
}

0 个答案:

没有答案