public class ShoppingCarNewAdapter extends BaseExpandableListAdapter {
private Context mCtx;
private ArrayList<ShopcarParentBean> shopcarList = new ArrayList<ShopcarParentBean>();
private onParentSelectListener parentListener;
private onItemSelectListener itemListener;
public void setOnItemSelectListener(onItemSelectListener itemListener) {
this.itemListener = itemListener;
}
public void setOnParentSelectListener(onParentSelectListener parentListener) {
this.parentListener = parentListener;
}
public ShoppingCarNewAdapter(Context context, ArrayList<ShopcarParentBean> list) {
this.mCtx = context;
this.shopcarList = list;
}
public void adapterChange(ArrayList<ShopcarParentBean> list) {
this.shopcarList = list;
this.notifyDataSetChanged();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return shopcarList.get(groupPosition).getShopCarItem().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int position, boolean arg2, View view, ViewGroup arg4) {
ChildViewHolder childVHolder = null;
if (view == null) {
view = LayoutInflater.from(mCtx).inflate(R.layout.adapter_shopping_car, null);
childVHolder = new ChildViewHolder(view);
view.setTag(childVHolder);
} else {
childVHolder = (ChildViewHolder) view.getTag();
}
// 捆绑数据
final ShopcarBean shopcarBean = (ShopcarBean) getChild(groupPosition, position);
childVHolder.costTv.setText(shopcarBean.getCash());
childVHolder.orderNoTv.setText(shopcarBean.getNum());
if (shopcarList.get(groupPosition).isSelect() || shopcarBean.isSelect()) {
childVHolder.checkBox.setChecked(true);
} else {
childVHolder.checkBox.setChecked(false);
}
childVHolder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CheckBox checkBox = (CheckBox) arg0;
shopcarBean.setIsSelect(checkBox.isChecked());
if (itemListener != null) {
itemListener.setItemSelect(shopcarBean, groupPosition);
}
}
});
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return shopcarList.get(groupPosition).getShopCarItem().size();// 二级分类的数量
}
@Override
public Object getGroup(int arg0) {
return shopcarList.get(arg0);
}
@Override
public int getGroupCount() {
return shopcarList.size();// 一级分类的数量
}
@Override
public long getGroupId(int arg0) {
return arg0;
}
@Override
public View getGroupView(int postion, boolean arg1, View view, ViewGroup arg3) {
ParentViewHolder parentVHolder = null;
if (null == view) {
view = LayoutInflater.from(mCtx).inflate(R.layout.adapter_shopping_car_parent, null);
parentVHolder = new ParentViewHolder(view);
view.setTag(parentVHolder);
} else {
parentVHolder = (ParentViewHolder) view.getTag();
}
// 捆绑数据
final ShopcarParentBean bean = shopcarList.get(postion);
parentVHolder.shopNameTv.setText(bean.getShopName());
parentVHolder.totalMoneyTv.setText(bean.getTotalMoney());
if (bean.isSelect()) {
parentVHolder.parentCheckBox.setChecked(true);
} else {
parentVHolder.parentCheckBox.setChecked(false);
}
parentVHolder.parentCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CheckBox pBox = (CheckBox) arg0;
bean.setIsSelect(pBox.isChecked());
if (parentListener != null) {
parentListener.setParentSelect(bean);
}
}
});
return view;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
static class ParentViewHolder {
@Bind(R.id.adapter_shopping_car_total_money)
TextView totalMoneyTv;
@Bind(R.id.adapter_shop_car_name)
TextView shopNameTv;
@Bind(R.id.adapter_shopping_car_parent_check_box)
CheckBox parentCheckBox;
public ParentViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
static class ChildViewHolder {
@Bind(R.id.adapter_shopping_car_orderno)
TextView orderNoTv;
@Bind(R.id.adapter_shopping_car_cost)
TextView costTv;
// @Bind(R.id.adapter_shopping_car_content_tv)
// TextView contentTv;
@Bind(R.id.adapter_shopping_car_check_box)
CheckBox checkBox;
public ChildViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
public interface onParentSelectListener {
public void setParentSelect(ShopcarParentBean shopCar);
}
public interface onItemSelectListener {
public void setItemSelect(ShopcarBean shopCar, int parentPos);
}
}
ExpandableList的用法
最新推荐文章于 2023-05-13 11:19:20 发布