检查是否选中了可扩展列表视图中每个子视图的至少一个复选框项

时间:2017-07-13 06:35:46

标签: java android listview

我有一个可扩展的ListView。这样的事情。

enter image description here

现在,只有在每行至少选择一个项目时才需要启用“继续”按钮。这有可能检查吗?

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fb="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ExpandableListView
        android:id="@+id/expandableListViewCreateProfileFecets"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/size_small"
        android:layout_marginLeft="@dimen/size_medium"
        android:layout_marginRight="@dimen/size_medium"
        android:layout_marginTop="@dimen/size_small"
        android:layout_weight="1"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:groupIndicator="@null" />

    <Button
        android:id="@+id/buttonContinue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/size_medium"
        android:background="@color/layoutGrey"
        android:enabled="true"
        android:letterSpacing="0.1"
        android:padding="@dimen/size_large"
        android:text="@string/button_continue"
        android:textColor="@color/white" />

</LinearLayout>

群组适配器

public class CreateProfileExpandableAdapter extends BaseExpandableListAdapter {

    ArrayList<CreateProfileFacetsHeaderModel> al;
    Activity activity;
    public CreateProfileExpandableAdapter(Activity activity, ArrayList<CreateProfileFacetsHeaderModel> al) {
        this.activity = activity;
        this.al = al;
        LinksAndKeys.arrayListSelectedFecets = al;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return al.get(groupPosition).getValues();
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return al.get(groupPosition).hashCode();
    }
    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        View v = convertView;
        String viewType = al.get(groupPosition).getAppearance();
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.create_profile_child_grid, parent, false);
        }
        ExpandableHeightGridView gridViewChildren = (ExpandableHeightGridView) v.findViewById(R.id.gridViewChildren);
        gridViewChildren.setExpanded(true);
        if (viewType.equalsIgnoreCase(LinksAndKeys.SIZE_LIST_KEY)) {
            gridViewChildren.setNumColumns(5);
            CreateProfileChildAdapterGrid createProfileChildAdapter = new CreateProfileChildAdapterGrid(activity, al.get(groupPosition).getValues(), groupPosition);
            gridViewChildren.setAdapter(createProfileChildAdapter);
        } else if (viewType.equalsIgnoreCase(LinksAndKeys.STYLES_LIST_KEY)) {
            gridViewChildren.setNumColumns(1);
            CreateProfileChildAdapterCheckBox createProfileChildAdapter = new CreateProfileChildAdapterCheckBox(activity, al.get(groupPosition).getValues(), groupPosition);
            gridViewChildren.setAdapter(createProfileChildAdapter);
        }
        return v;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }
    @Override
    public Object getGroup(int groupPosition) {
        return al.get(groupPosition);
    }
    @Override
    public int getGroupCount() {
        return al.size();
    }
    @Override
    public long getGroupId(int groupPosition) {
        return al.get(groupPosition).hashCode();
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.create_profile_group, parent, false);
        }
        TextView textViewGroup = (TextView) v.findViewById(R.id.textViewGroup);
        ImageView imageViewExpanded = (ImageView) v.findViewById(R.id.imageViewExpanded);
        textViewGroup.setText(al.get(groupPosition).getTitle());
        if (isExpanded)
            imageViewExpanded.setImageResource(R.drawable.minus);
        else
            imageViewExpanded.setImageResource(R.drawable.plus);
        return v;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

儿童适配器:

public class CreateProfileChildAdapterGrid extends BaseAdapter {

    Activity activity;
    ArrayList<CreateProfileFacetsChildModel> al;
    int index = -1;
    int groupPosition;

    public CreateProfileChildAdapterGrid(Activity activity, ArrayList<CreateProfileFacetsChildModel> al, int groupPosition) {
        // TODO Auto-generated constructor stub
        this.activity = activity;
        this.al = al;
        this.groupPosition = groupPosition;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return al.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return al.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    private static class ViewHolder {
        CheckBox checkBoxChild;
    }

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

        final ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(activity);
            convertView = inflater.inflate(R.layout.create_profile_grid_child, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.checkBoxChild = (CheckBox) convertView.findViewById(R.id.checkBoxChild);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.checkBoxChild.setText(al.get(position).getDefaultValue());
        viewHolder.checkBoxChild.setChecked(al.get(position).isSelected());
        if (al.get(position).isSelected())
            viewHolder.checkBoxChild.setTextColor(ContextCompat.getColor(activity, R.color.white));
        else
            viewHolder.checkBoxChild.setTextColor(ContextCompat.getColor(activity, R.color.black));

        viewHolder.checkBoxChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                al.get(position).setSelected(b);
                LinksAndKeys.arrayListSelectedFecets.get(groupPosition).getValues().get(position).setSelected(b);

                if (b) {
                    viewHolder.checkBoxChild.setTextColor(ContextCompat.getColor(activity, R.color.white));
                } else {
                    viewHolder.checkBoxChild.setTextColor(ContextCompat.getColor(activity, R.color.black));
                }
            }
        });

        return convertView;
    }

}

0 个答案:

没有答案