逻辑很简单,捋顺就好了~~~~~~~~
//全选方法
public void selsetAll(final List glist) {
//全选控制一级
cbAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked =cbAll.isChecked();
cbAll.setChecked(checked);
if(checked) {
for(int i =0; i<glist.size();i++){
glist.get(i).setG_ischecked(true);
for(int j =0; j<glist.get(i).getList().size();j++){
glist.get(i).getList().get(j).setC_ischecked(true);
}
}
}else{
for(inti =0; i<glist.size();i++){
glist.get(i).setG_ischecked(false);
for(intj =0; j<glist.get(i).getList().size();j++){
glist.get(i).getList().get(j).setC_ischecked(false);
}
}
}
adapter.notifyDataSetChanged();
sumPrice(glist);
}
});
}
/**
* ExpandableAdapter适配器
* 2.@authorDell
* 3.@date2017/9/14 13:41
*/
public class ExpandableAdapter extends BaseExpandableListAdapter{
private List glist;
private Context context;
public ExpandableAdapter(List glist, Context context) {
this.glist= glist;
this.context= context;
}
@Override
public int getGroupCount() {
return glist.size();
}
@Override
public int getChildrenCount(intgroupPosition) {
return glist.get(groupPosition).getList().size();
}
@Override
public Object getGroup(intgroupPosition) {
return glist.get(groupPosition);
}
@Override
public Object getChild(intgroupPosition,intchildPosition) {
return glist.get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(intgroupPosition) {
return groupPosition;
}
@Override
public long getChildId(intgroupPosition,intchildPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final intgroupPosition,booleanisExpanded, View convertView, ViewGroup parent) {
final GroupViewHolder gh;
if(convertView==null){
gh=new GroupViewHolder();
convertView=View.inflate(context, R.layout.expandable_group,null);
gh.cb_group= (CheckBox) convertView.findViewById(R.id.cb_group);
gh.tv_group_name= (TextView) convertView.findViewById(R.id.tv_group_name);
convertView.setTag(gh);
}else{
gh= (GroupViewHolder) convertView.getTag();
}
gh.cb_group.setChecked(glist.get(groupPosition).isG_ischecked());
gh.tv_group_name.setText(glist.get(groupPosition).getSellerName());
gh.cb_group.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked =gh.cb_group.isChecked();
if(checked){
glist.get(groupPosition).setG_ischecked(true);
}else{
glist.get(groupPosition).setG_ischecked(false);
}
//判断一级选中个数
int n=0;
for(inti =0; i<glist.size();i++){
booleang_ischecked =glist.get(i).isG_ischecked();
if(g_ischecked){
for(int j =0; j<glist.get(i).getList().size();j++){
glist.get(i).getList().get(j).setC_ischecked(true);
}
n++;
}else{
for(int j =0; j<glist.get(i).getList().size;j++){
glist.get(i).getList().get(j).setC_ischecked(false);
}
}
}
//一级控制全选
if(n==glist.size()){
onCheckedClick.setOnCheckedClick(true);
}else{
onCheckedClick.setOnCheckedClick(false);
}
sumPrice();
notifyDataSetChanged();
}
});
returnconvertView;
}
@Override
publicView getChildView(final intgroupPosition,final intchildPosition,booleanisLastChild, View convertView, ViewGroup parent) {
final ChildViewHolder ch;
if(convertView==null){
ch=newChildViewHolder();
convertView=View.inflate(context,R.layout.expandable_child,null);
ch.cb_child= (CheckBox) convertView.findViewById(R.id.cb_child);
ch.iv_child_image= (ImageView) convertView.findViewById(R.id.iv_child_image);
ch.tv_child_name= (TextView) convertView.findViewById(R.id.tv_child_name);
ch.tv_child_price= (TextView) convertView.findViewById(R.id.tv_child_price);
ch.child_number= (TextView) convertView.findViewById(R.id.child_number);
convertView.setTag(ch);
}else{
ch= (ChildViewHolder) convertView.getTag();
}
//适配数据
ch.cb_child.setChecked(glist.get(groupPosition).getList().get(childPosition).isC_ischecked());
String[] split =glist.get(groupPosition).getList().get(childPosition).getImages().split("\\|");
Picasso.with(context).load(split[0]).into(ch.iv_child_image);
ch.tv_child_name.setText(glist.get(groupPosition).getList().get(childPosition).getTitle());
doubleprice =glist.get(groupPosition).getList().get(childPosition).getPrice();
ch.tv_child_price.setText("¥"+glist.get(groupPosition).getList().get(childPosition).getPrice()+"");
ch.child_number.setText(glist.get(groupPosition).getList().get(childPosition).getNum()+"");
ch.cb_child.setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked =ch.cb_child.isChecked();
if(checked){
glist.get(groupPosition).getList().get(childPosition).setC_ischecked(true);
}else{
glist.get(groupPosition).getList().get(childPosition).setC_ischecked(false);
}
//判断二级选中的个数
int n=0;
for(int i =0; i<glist.get(groupPosition).getList().size();i++){
boolean c_ischecked =glist.get(groupPosition).getList().get(i).isC_ischecked();
if(c_ischecked){
n++;
}
}
//二级控制一级
if(n==glist.get(groupPosition).getList().size()){
glist.get(groupPosition).setG_ischecked(true);
}else{
glist.get(groupPosition).setG_ischecked(false);
}
//判断一级选中个数
int m=0;
for(int i =0; i<glist.size();i++){
booleang_ischecked =glist.get(i).isG_ischecked();
if(g_ischecked){
m++;
}
}
//一级控制全选
if(m==glist.size()){
onCheckedClick.setOnCheckedClick(true);
}else{
onCheckedClick.setOnCheckedClick(false);
}
sumPrice();
notifyDataSetChanged();
}
});
return convert View;
}
@Override
public boolean isChildSelectable(int groupPosition,int childPosition) {
return true;
}
class GroupViewHolder{
CheckBox cb_group;
TextView tv_group_name;
}
class ChildViewHolder{
CheckBox cb_child;
ImageView iv_child_image;
TextView tv_child_name;
TextView tv_child_price;
ImageView child_jian,child_jia;
TextView child_number;
}
//接口回调
public OnCheckedClick onCheckedClick;
public void setOnCheckedClick(OnCheckedClick onCheckedClick) {
this.onCheckedClick= onCheckedClick;
}
//传递价格、数量的接口
public interface OnCheckedClick{
void setOnCheckedClick(booleanflag);
void setOnCheckedCount(intcount);
void setOnCheckedPrice(doubleprice);
}
//算价格数量的方法
public void sumPrice(){
int count=0;
double price=0.0;
for(inti =0; i<glist.size();i++){
for(intj =0; j<glist.get(i).getList().size(); j++){
boolean c_ischecked =glist.get(i).getList().get(j).isC_ischecked();
if(c_ischecked){
int num = Integer.parseInt(glist.get(i).getList().get(j).getNum()+"");
double v = Double.parseDouble(glist.get(i).getList().get(j).getPrice()+"");
count+=num;
price+=v*num;
}
}
}
onCheckedClick.setOnCheckedCount(count);
onCheckedClick.setOnCheckedPrice(price);
}
}