先来个布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.activity.MainActivity">
<!--<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/reclayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>-->
<ExpandableListView
android:id="@+id/expanded_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/rmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥ 0.0"
/>
</LinearLayout>
</RelativeLayout>
子布局
这里 android:focusable=“false”
android:clickable="true"必须写
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<CheckBox
android:id="@+id/select_item"
android:layout_width="wrap_content"
android:focusable="false"
android:clickable="true"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/select_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
public class ShoopCartAdapter extends BaseExpandableListAdapter {
private Context context;
private ShoopBean shoopBean;
public ShoopCartAdapter(Context context) {
this.context = context;
}
public void setData(ShoopBean shoopBean) {
this.shoopBean = shoopBean;
}
@Override
public int getGroupCount() {
if (shoopBean == null) {
return 0;
}
return shoopBean.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
if (shoopBean == null) {
return 0;
} else {
return shoopBean.getData().get(groupPosition).getSpus().size();
}
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null) {
groupViewHolder = new GroupViewHolder();
convertView = View.inflate(context, R.layout.grouplayout, null);
groupViewHolder.name = convertView.findViewById(R.id.name);
groupViewHolder.checkBox = convertView.findViewById(R.id.select_item);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.name.setText(shoopBean.getData().get(groupPosition).getName());
groupViewHolder.checkBox.setChecked(shoopBean.getData().get(groupPosition).isChecked());
groupViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkB = (CheckBox) v;
boolean checked = checkB.isChecked();
groupselect(checked,groupPosition);
}
});
return convertView;
}
private void groupselect(boolean checked, int groupPosition) {
ShoopBean.DataBean dataBean = shoopBean.getData().get(groupPosition);
dataBean.setChecked(checked);
Log.e("xxxx", "" + checked);
for (int j = 0; j < dataBean.getSpus().size(); j++) {
ShoopBean.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
Log.e("ccccc", "" + checked);
spusBean.setChecked(checked);
}
notifyDataSetChanged();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if (convertView == null) {
childViewHolder = new ChildViewHolder();
convertView = View.inflate(context, R.layout.grouplayout, null);
childViewHolder.name = convertView.findViewById(R.id.name);
childViewHolder.checkBox = convertView.findViewById(R.id.select_item);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.name.setText(shoopBean.getData().get(groupPosition).getSpus().get(childPosition).getName());
childViewHolder.checkBox.setChecked(shoopBean.getData().get(groupPosition).getSpus().get(childPosition).isChecked());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private CheckBox mcheckBox;
public void setCheckBox(final CheckBox checkBox) {
this.mcheckBox = checkBox;
mcheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkB = (CheckBox) v;
boolean checked = checkB.isChecked();
selectAll(checked);
}
});
}
private void selectAll(boolean checked) {
for (int i = 0; i < shoopBean.getData().size(); i++) {
ShoopBean.DataBean dataBean = shoopBean.getData().get(i);
dataBean.setChecked(checked);
for (int j = 0; j < dataBean.getSpus().size(); j++) {
ShoopBean.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
spusBean.setChecked(checked);
}
}
notifyDataSetChanged();
}
class GroupViewHolder {
TextView name;
CheckBox checkBox;
}
class ChildViewHolder {
TextView name;
CheckBox checkBox;
}
}