packagecom.bwie.app.adapter;
importandroid.content.Context;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseExpandableListAdapter;
importandroid.widget.Button;
importandroid.widget.CheckBox;
importandroid.widget.ImageView;
importandroid.widget.TextView;
importcom.bwie.app.R;
importcom.bwie.app.bean.Goods;
importcom.bwie.app.bean.Shopper;
importcom.squareup.picasso.Picasso;
importjava.util.List;
/**
* 1. 类的用途
* 2. @author 适配器
* 3. @date 2017/10/19 14:19
*/
public classCarAdapterextendsBaseExpandableListAdapter {
private Context context;//上下文
private List glist;//商家集合
private List> clist;//商品集合
private IListener listener;//接口回调声明
public CarAdapter(Context context, List glist, List> clist) {
this.context= context;
this.glist= glist;
this.clist= clist;
}
@Override
public intgetGroupCount() {
returnglist.size();
}
@Override
public intgetChildrenCount(intgroupPosition) {
returnclist.get(groupPosition).size();
}
@Override
publicObject getGroup(intgroupPosition) {
returnglist.get(groupPosition);
}
@Override
publicObject getChild(intgroupPosition,intchildPosition) {
returnclist.get(groupPosition).get(childPosition);
}
@Override
public longgetGroupId(intgroupPosition) {
returngroupPosition;
}
@Override
public longgetChildId(intgroupPosition,intchildPosition) {
returnchildPosition;
}
@Override
public booleanhasStableIds() {
return true;
}
@Override
publicView getGroupView(final intgroupPosition,booleanisExpanded, View convertView, ViewGroup parent) {
finalGHolder gHolder;
if(convertView==null){
gHolder=newGHolder();
convertView=View.inflate(context, R.layout.gitem,null);
gHolder.gcheck= (CheckBox) convertView.findViewById(R.id.gcheck);
gHolder.shopper= (TextView) convertView.findViewById(R.id.shoppername);
convertView.setTag(gHolder);
}else{
gHolder= (GHolder) convertView.getTag();
}
gHolder.gcheck.setChecked(glist.get(groupPosition).ischeck());
gHolder.shopper.setText(glist.get(groupPosition).getSellerName());
//二级控制一级
gHolder.gcheck.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
boolean checked =gHolder.gcheck.isChecked();
//一级控制二级
if(checked){
glist.get(groupPosition).setIscheck(true);
List goodses =clist.get(groupPosition);
for(int i=0;i<goodses.size();i++){
goodses.get(i).setIscheck(true);
}
}else{
glist.get(groupPosition).setIscheck(false);
List goodses =clist.get(groupPosition);
for(int i=0;i
goodses.get(i).setIscheck(false);
}
}
//一级控制全选
intnum=0;
for(inti=0;i
if(glist.get(i).ischeck()){
num++;
}
}
if(num==glist.size()){
listener.setCheck(true);
}else{
listener.setCheck(false);
}
notifyDataSetChanged();
js();
}
});
returnconvertView;
}
@Override
publicView getChildView(final intgroupPosition,final intchildPosition,booleanisLastChild, View convertView, ViewGroup parent) {
finalCHolder cHolder;
if(convertView==null){
cHolder=newCHolder();
convertView=View.inflate(context,R.layout.citem,null);
cHolder.ccheck= (CheckBox) convertView.findViewById(R.id.ccheck);
cHolder.name= (TextView) convertView.findViewById(R.id.tv_title);
cHolder.price= (TextView) convertView.findViewById(R.id.tv_price);
cHolder.num= (TextView) convertView.findViewById(R.id.num);
cHolder.imageView= (ImageView) convertView.findViewById(R.id.iv_pic);
cHolder.jia= (Button) convertView.findViewById(R.id.jia);
cHolder.jian= (Button) convertView.findViewById(R.id.jian);
convertView.setTag(cHolder);
}else{
cHolder= (CHolder) convertView.getTag();
}
cHolder.ccheck.setChecked(clist.get(groupPosition).get(childPosition).ischeck());
Picasso.with(context).load(clist.get(groupPosition).get(childPosition).getImage()).into(cHolder.imageView);
cHolder.name.setText(clist.get(groupPosition).get(childPosition).getTitle());
cHolder.price.setText(clist.get(groupPosition).get(childPosition).getPrice()+"");
cHolder.num.setText(clist.get(groupPosition).get(childPosition).getNum()+"");
cHolder.ccheck.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
booleanchecked =cHolder.ccheck.isChecked();
if(checked){
clist.get(groupPosition).get(childPosition).setIscheck(true);
}else{
clist.get(groupPosition).get(childPosition).setIscheck(false);
}
//二级全选,一级全选
intnum=0;
for(inti=0;i
if(clist.get(groupPosition).get(i).ischeck()){
num++;
}
}
if(num==clist.get(groupPosition).size()){
glist.get(groupPosition).setIscheck(true);
}else{
glist.get(groupPosition).setIscheck(false);
}
//二级控制全选
intn=0;
for(inti=0;i
if(glist.get(i).ischeck()){
n++;
}
}
if(n==glist.size()){
listener.setCheck(true);
}else{
listener.setCheck(false);
}
notifyDataSetChanged();
js();
}
});
returnconvertView;
}
@Override
public booleanisChildSelectable(intgroupPosition,intchildPosition) {
return true;
}
classGHolder{
CheckBoxgcheck;
TextViewshopper;
}
classCHolder{
CheckBoxccheck;
TextViewname,price,num;
ImageViewimageView;
Buttonjia,jian;
}
//计算价钱和数量
public voidjs(){
intnum=0;//数量
doubleprice=0;//价钱
for(inti=0;i
List list =clist.get(i);
for(intj=0;j
if(list.get(j).ischeck()){
num+=list.get(j).getNum();
price+=list.get(j).getPrice()*list.get(j).getNum();
}
}
listener.setNum(num);
listener.setPrice(price);
}
};
//接口回调
public interfaceIListener{
//设置价格
voidsetPrice(doubleprice);
//设置数量
voidsetNum(intnum);
//设置全选监听
voidsetCheck(booleanischeck);
}
public voidsetListener(IListener listener) {
this.listener= listener;
}
}
//点击全选所有商品选中
booleanchecked =both.isChecked();
if(checked){
for(inti=0;i
glist.get(i).setIscheck(true);
}
for(inti=0;i
List goodsList =clist.get(i);
for(intj=0;j
goodsList.get(j).setIscheck(true);
}
}
}else{
for(inti=0;i
glist.get(i).setIscheck(false);
}
for(inti=0;i
List goodsList =clist.get(i);
for(intj=0;j
goodsList.get(j).setIscheck(false);
}
}
}
js();
adapter.notifyDataSetChanged();