可扩展列表视图子项目单击不起作用

时间:2016-01-13 03:56:24

标签: android expandablelistview expandablelistadapter

在我的可扩展列表视图中,第一个子项有两个buttons.button1和button2.in其他子列表项有复选框和text.these复选框默认禁用 在第一次用户展开子列表后,按下按钮1我想启用特定组中的所有复选框

至此,我只能启用第一个子列表项checkbox.how可以通过单击第一个子项上的按钮来启用所有复选框。

可扩展列表适配器

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<ExpandableListGroup> groups;

public ExpandListAdapter(Context context, ArrayList<ExpandableListGroup> groups) {
    this.context = context;
    this.groups = groups;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
    return chList.get(childPosition);
}

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

@Override
public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final ExpandableListChild child = (ExpandableListChild) getChild(groupPosition, childPosition);
    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);

        holder = new ViewHolder();

        holder.btn_start_inspection = (Button) convertView.findViewById(R.id.btn_start_inspection);
        holder.btn_complete_inspection = (Button) convertView.findViewById(R.id.btn_complete_inspection);
        holder.cbx_complete_inspection = (CheckBox) convertView.findViewById(R.id.check_sub_task);
        holder.txt_activity_name = (TextView) convertView.findViewById(R.id.txt_sub_task_title);

        holder.txt_activity_name.setText(child.getName().toString());

        //button for enable checkboxes
        holder.btn_start_inspection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.btn_complete_inspection.setBackgroundColor(Color.RED);
                holder.itemEnabled = true;
                notifyDataSetChanged();
            }
        });

        //enable check boxes
        if(holder.itemEnabled == true){
            holder.cbx_complete_inspection.setEnabled(true);
        }else{
            holder.cbx_complete_inspection.setEnabled(false);
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}


@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
    return chList.size();
}

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

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

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    ExpandableListGroup group = (ExpandableListGroup) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.list_group, null);
    }

    TextView tv = (TextView) convertView.findViewById(R.id.txt_company_name);
    tv.setText(group.getName());

    TextView tvAddress = (TextView) convertView.findViewById(R.id.txt_company_address);
    tvAddress.setText(group.getAddressLevel());

    iv = (ImageView) convertView.findViewById(R.id.img_expand);

    return convertView;
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
    iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_fold));
}

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
    iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_min));

}

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

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

static class ViewHolder {
    Button btn_complete_inspection;
    Button btn_start_inspection;
    CheckBox cbx_complete_inspection;
    TextView txt_activity_name;
    boolean itemEnabled = false;
}

}

listchildItem.xml

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

    <RelativeLayout
        android:id="@+id/lnr_task"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="10dp">

        <Button

            android:id="@+id/btn_start_inspection"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_gravity="left"
            android:layout_marginLeft="10dp"
            android:background="@drawable/rounded_cornerexpandable_list_child"
            android:gravity="center"
            android:text="Start Inspection"
            android:textAllCaps="false"
            android:textColor="@color/color_white"
            android:textSize="12sp"
            ></Button>


        <Button
            android:id="@+id/btn_complete_inspection"
            android:layout_width="150dp"
            android:layout_height="35dp"
            android:layout_marginLeft="150dp"
            android:src="@drawable/rounded_cornerexpandable_list_child_gray"
            android:text="Complete Inspection"
            android:textAllCaps="false"
            android:textColor="@color/color_white"
            android:textSize="12sp"
            ></Button>

        <LinearLayout
            android:id="@+id/lnr_active_sub_task"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@+id/btn_complete_inspection">

            <LinearLayout
                android:id="@+id/lnr_sub_task"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:gravity="center"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/check_sub_task"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:checked="false"
                    android:gravity="center"></CheckBox>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/textHolder"
                android:layout_width="0.0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:gravity="left"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/txt_sub_task_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="exx"
                    android:textSize="15sp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0.0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_weight="0.5"
                android:gravity="right"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/txt_sub_task_count"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ic_keyboard_arrow_right_black_24dp"
                    android:gravity="center"/>

            </LinearLayout>

        </LinearLayout>


    </RelativeLayout>


</LinearLayout>

我看到的是,在第一个列表项目上单击按钮后,仅启用第一个子项目中的第一个复选框,而不启用其他列表项复选框。我想要的是启用子项行中的复选框。 (从评论中添加额外的行)

0 个答案:

没有答案