簡單的二級購物車(2)
阿新 • • 發佈:2018-11-19
6.自定義的包
6.1 —CarTitleView
public class CarTitleView extends LinearLayout implements View.OnClickListener{
private CarTitleInterface carTitleInterface;
private ImageView title_address;
private TextView tv_compile;
private ImageView iv_message;
public void setCarTitleView (CarTitleInterface carTitleInterface){
this.carTitleInterface = carTitleInterface;
}
public CarTitleView(Context context) {
this(context,null);
}
public CarTitleView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public CarTitleView (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View.inflate(context, R.layout.car_title_fragment,this);
initView();
}
private void initView() {
title_address = (ImageView)findViewById(R.id.title_address);
tv_compile = (TextView)findViewById(R.id.tv_compile);
iv_message = (ImageView)findViewById(R.id.iv_message);
title_address.setOnClickListener(this );
tv_compile.setOnClickListener(this);
iv_message.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.title_address:
carTitleInterface.addressClick(view);
break;
case R.id.tv_compile:
carTitleInterface.compileClick(view);
break;
case R.id.iv_message:
carTitleInterface.messageClick(view);
break;
}
}
}
6.2 – CarShopCheckedInterface
public interface CarShopCheckedInterface {
void AllChecked(boolean ischeck);
}
6.3–CarTitleInterface
public interface CarTitleInterface {
void addressClick(View v);
void compileClick(View v);
void messageClick(View v);
}
6.4–NumberAndPrice
public interface NumberAndPrice {
void AllNumAndPriceClick(ArrayList<List<Car_child_shop_Bean>> list_shop_child);
}
6.5–ShopNumInterface
public interface ShopNumInterface {
void ListClick(List<Car_child_shop_Bean> car_child_shop_been);
}
7.介面卡 CarShopAdapter
public class CarShopAdapter extends BaseExpandableListAdapter{
private Context context;
private ArrayList<Car_shop_Bean> list_shop;
private ArrayList<List<Car_child_shop_Bean>> list_shop_child;
private CarShopCheckedInterface carShopCheckedInterface;
private ShopNumInterface shopNumInterface;
private NumberAndPrice numberAndPrice;
private TextView tv_shopName;
private ImageView iv_commodityImg;
private TextView tv_commodityName;
private TextView tv_commodityPrice;
private Button btn_delete;
private Button btn_add;
private EditText et_num;
private int shopNum = 0;
private int allNum = 0;
private int allPrice = 0;
private Button btn_delete_only;
public CarShopAdapter(Context context, ArrayList<Car_shop_Bean> list_shop, ArrayList<List<Car_child_shop_Bean>> list_shop_child) {
this.context = context;
this.list_shop = list_shop;
this.list_shop_child = list_shop_child;
}
public void setCarShop(CarShopCheckedInterface carShopCheckedInterface){
this.carShopCheckedInterface = carShopCheckedInterface;
}
public void setShopList(ShopNumInterface shopNumInterface){
this.shopNumInterface = shopNumInterface;
}
public void setNumberAndPrice(NumberAndPrice numberAndPrice){
this.numberAndPrice = numberAndPrice;
}
@Override
public int getGroupCount() {
return list_shop.size();
}
@Override
public int getChildrenCount(int i) {
return list_shop_child.get(i).size();
}
@Override
public Object getGroup(int i) {
return list_shop.get(i);
}
@Override
public Object getChild(int i, int i1) {
return list_shop_child.get(i).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final Car_shop_Bean car_shop_bean1 = list_shop.get(i);
final List<Car_child_shop_Bean> car_child_shop_been = list_shop_child.get(i);
view = View.inflate(context, R.layout.car_shop_one_fragment, null);
final CheckBox rb_shop = (CheckBox)view.findViewById(R.id.rb_shop);
tv_shopName = (TextView)view.findViewById(R.id.tv_shopName);
rb_shop.setChecked(car_shop_bean1.getFlag());
tv_shopName.setText(car_shop_bean1.getName());
rb_shop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("---------點選:",rb_shop.isChecked()+"");
list_shop.get(i).setFlag(rb_shop.isChecked());
boolean checked = rb_shop.isChecked();
car_shop_bean1.setFlag(checked);
boolean a = true;
for (int j = 0; j < list_shop.size(); j++) {
Boolean flag = list_shop.get(j).getFlag();
if(flag==false){
a = false;
carShopCheckedInterface.AllChecked(a);
break;
}else{
a = true;
carShopCheckedInterface.AllChecked(a);
}
}
for (int j = 0; j < car_child_shop_been.size(); j++) {
car_child_shop_been.get(j).setChildCheck(rb_shop.isChecked());
}
notifyDataSetChanged();
for (Car_child_shop_Bean list_child:car_child_shop_been){
numberAndPrice.AllNumAndPriceClick(list_shop_child);
}
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, final boolean b, View view, ViewGroup viewGroup) {
final Car_shop_Bean car_shop_bean = list_shop.get(i);
final Car_child_shop_Bean car_child_shop_bean = list_shop_child.get(i).get(i1);
view = View.inflate(context, R.layout.car_shop_fragment, null);
final CheckBox rb_commodity = (CheckBox)view.findViewById(R.id.rb_commodity);
iv_commodityImg = (ImageView)view.findViewById(R.id.iv_commodityImg);
tv_commodityName = (TextView)view.findViewById(R.id.tv_commodityName);
tv_commodityPrice = (TextView)view.findViewById(R.id.tv_commodityPrice);
btn_delete = (Button)view.findViewById(R.id.btn_delete);
btn_add = (Button)view.findViewById(R.id.btn_add);
et_num = (EditText)view.findViewById(R.id.et_num);
et_num.setText(car_child_shop_bean.getNum()+"");
rb_commodity.setChecked(car_child_shop_bean.isChildCheck());
tv_commodityName.setText(car_child_shop_bean.getTitle());
Glide.with(context).load(car_child_shop_bean.getImages()).into(iv_commodityImg);
tv_commodityPrice.setText("¥:"+car_child_shop_bean.getPrice());
btn_delete.setText("—");
btn_add.setText("+");
notifyDataSetChanged();
rb_commodity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
car_child_shop_bean.setChildCheck(rb_commodity.isChecked());
boolean flag = true;
for (int j = 0; j < list_shop_child.get(i).size(); j++) {
if(list_shop_child.get(i).get(j).isChildCheck()==false) {
flag = false;
break;
}
}
if(flag){
list_shop.get(i).setFlag(true);
}else{
list_shop.get(i).setFlag(false);
}
boolean c = true;
for (int j = 0; j < list_shop.size(); j++) {
if(list_shop.get(j).getFlag() == false){
c = false;
break;
}else{
c = true;
}
}
if(c){
carShopCheckedInterface.AllChecked(true);
}else{
carShopCheckedInterface.AllChecked(false);
}
Log.e("-----取到的索引:",i1+"");
numberAndPrice.AllNumAndPriceClick(list_shop_child);
notifyDataSetChanged();
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean childCheck = car_child_shop_bean.isChildCheck();
int num1 = car_child_shop_bean.getNum();
num1++;
car_child_shop_bean.setNum(num1);
numberAndPrice.AllNumAndPriceClick(list_shop_child);
notifyDataSetChanged();
}
});
final boolean childCheck = car_child_shop_bean.isChildCheck();
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1 = car_child_shop_bean.getNum();
num1--;
if(num1<1){
car_child_shop_bean.setNum(1);
Toast.makeText(context,"不能再少了···",Toast.LENGTH_SHORT).show();
}else{
car_child_shop_bean.setNum(num1);
}
numberAndPrice.AllNumAndPriceClick(list_shop_child);
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
8.MainActivity(fn+alt_INS 找空件)
public class MainActivity extends AppCompatActivity implements GwcShowV {
@BindView(R.id.pt_lv)
ExpandableListView pt_lv;
@BindView(R.id.btn_toBuy)
Button btn_toBuy;
@BindView(R.id.ll_toBuy)
LinearLayout ll_toBuy;
@BindView(R.id.cb_all)
CheckBox cb_all;
@BindView(R.id.tv_rental)
TextView tv_rental;
@BindView(R.id.tv_num)
TextView tv_num;
@BindView(R.id.btn_goto)
Button btn_goto;
@BindView(R.id.lll_one)
LinearLayout lll_one;
@BindView(R.id.cb_alldel)
CheckBox cb_alldel;
@BindView(R.id.btn_delete_all)
Button btn_delete_all;
@BindView(R.id.l_two)
LinearLayout l_two;
@BindView(R.id.ctv_title)
CarTitleView ctv_title;
private ArrayList<Car_shop_Bean> list_shop;
private ArrayList<List<Car_child_shop_Bean>> list_shop_chiled;
private CarShopAdapter carShopAdapter;
private Unbinder unbinder;
private GwcShowPatenter gwcShowPatenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
private void initView() {
initTitle();
// initSelCar();
initDelCar();
}
private void initDelCar() {
}
private void initSelCar() {
HashMap<String, String> map = new HashMap<>();
/* String uid = MyApp.yhsp.getString("uid", "");*/
map.put("uid", "" +3428);
gwcShowPatenter = new GwcShowPatenter(this, map);
gwcShowPatenter.pdModleOne();
}
//頭佈局的點選事件
private void initTitle() {
ctv_title.setCarTitleView(new CarTitleInterface() {
boolean b = true;
public TextView tv_compile;
@Override
public void addressClick(View v) {
}
//點選編輯----
@Override
public void compileClick(View v) {
b = !b;
tv_compile = (TextView) v.findViewById(R.id.tv_compile);
if (b) {
tv_compile.setText("編輯");
lll_one.setVisibility(View.VISIBLE);
l_two.setVisibility(View.GONE);
carShopAdapter.notifyDataSetChanged();
} else {
tv_compile.setText("完成");
lll_one.setVisibility(View.GONE);
l_two.setVisibility(View.VISIBLE);
carShopAdapter.notifyDataSetChanged();
}
}
@Override
public void messageClick(View v) {
}
});
}
//點選事件
@OnClick({R.id.btn_toBuy, R.id.cb_all, R.id.btn_goto, R.id.cb_alldel, R.id.btn_delete_all})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_toBuy:
break;
case R.id.cb_all:
boolean checked = cb_all.isChecked();
for (int i = 0; i < list_shop.size(); i++) {
list_shop.get(i).setFlag(checked);
}
for (List<Car_child_shop_Bean> i : list_shop_chiled) {
for (int j = 0; j < i.size(); j++) {
i.get(j).setChildCheck(checked);
}
}
carShopAdapter.notifyDataSetChanged();
foreach(list_shop_chiled);
break;
case R.id.btn_goto:
break;
case R.id.cb_alldel:
boolean checkeds = cb_alldel.isChecked();
for (int i = 0; i < list_shop.size(); i++) {
list_shop.get(i).setFlag(checkeds);
}
for (List<Car_child_shop_Bean> i : list_shop_chiled) {
for (int j = 0; j < i.size(); j++) {
i.get(j).setChildCheck(checkeds);
}
}
carShopAdapter.notifyDataSetChanged();
break;
//點選刪除
case R.id.btn_delete_all:
ArrayList<Car_shop_Bean> list_new_shop = new ArrayList<Car_shop_Bean>();
ArrayList<List<Car_child_shop_Bean>> list_new_child = new ArrayList<List<Car_child_shop_Bean>>();
for (int i = 0; i < list_shop.size(); i++) {
Car_shop_Bean car_shop_bean = list_shop.get(i);
List<Car_child_shop_Bean> car_child_shop_been = list_shop_chiled.get(i);
Boolean flag = list_shop.get(i).getFlag();
if (flag) {
list_new_shop.add(car_shop_bean);
list_new_child.add(car_child_shop_been);
}
ArrayList<Car_child_shop_Bean> list_child_new = new ArrayList<Car_child_shop_Bean>();
for (int j = 0; j < list_shop_chiled.get(i).size(); j++) {
Car_child_shop_Bean car_child_shop_bean = list_shop_chiled.get(i).get(j);
boolean childCheck = list_shop_chiled.get(i).get(j).isChildCheck();
if (childCheck) {
list_child_new.add(car_child_shop_bean);
int pid = car_child_shop_bean.getPid();
HashMap<String, String> map = new HashMap<String, String>();
map.put("pid", pid + "");
// String uid = MyApp.yhsp.getString("uid", "");
map.put("uid", "" + 3428);
// Persent persent = new Persent(getActivity());
//persent.getData(map, "http://120.27.23.105/product/", 1);
gwcShowPatenter = new GwcShowPatenter(this, map);
gwcShowPatenter.pdModleDel();
}
}
list_shop_chiled.get(i).removeAll(list_child_new);
}
tv_rental.setText("合計:¥" + 0);
tv_num.setText("數量:" + 0);
list_shop.removeAll(list_new_shop);
list_shop_chiled.removeAll(list_new_child);
carShopAdapter.notifyDataSetChanged();
// initAllData(list_shop);
break;
}
}
//查詢成功的資料
@Override
public void ShowSuccessGwcv(GwcBean gwcBean) {
if (gwcBean != null) {
List<GwcBean.DataBean> data = gwcBean.getData();
if (data != null) {
list_shop = new ArrayList<Car_shop_Bean>();
list_shop_chiled = new ArrayList<List<Car_child_shop_Bean>>();
//設定選擇狀態
for (int i = 0; i < data.size(); i++) {
String sellerName = data.get(i).getSellerName();
list_shop.add(new Car_shop_Bean(sellerName, false));
List<GwcBean.DataBean.ListBean> list = data.get(i).getList();
ArrayList<Car_child_shop_Bean> car_shop_been = new ArrayList<>();
for (GwcBean.DataBean.ListBean childList : list) {
int num = childList.getNum();
double price = childList.getPrice();
String images = childList.getImages();
String title = childList.getTitle();
int pid = childList.getPid();
String[] split = images.toString().split("\\|");
car_shop_been.add(new Car_child_shop_Bean(pid, title, num, price, split[0], false));
}
list_shop_chiled.add(car_shop_been);
}
//initAllData(list_shop);
//設定介面卡
carShopAdapter = new CarShopAdapter(this, list_shop, list_shop_chiled);
cb_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = cb_all.isChecked();
for (int i = 0; i < list_shop.size(); i++) {
list_shop.get(i).setFlag(checked);
}
for (List<Car_child_shop_Bean> i : list_shop_chiled) {
for (int j = 0; j < i.size(); j++) {
i.get(j).setChildCheck(checked);
}
}
carShopAdapter.notifyDataSetChanged();
foreach(list_shop_chiled);
}
});
cb_alldel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = cb_alldel.isChecked();
for (int i = 0; i < list_shop.size(); i++) {
list_shop.get(i).setFlag(checked);
}
for (List<Car_child_shop_Bean> i : list_shop_chiled) {
for (int j = 0; j < i.size(); j++) {
i.get(j).setChildCheck(checked);
}
}
carShopAdapter.notifyDataSetChanged();
}
});
carShopAdapter.setCarShop(new CarShopCheckedInterface() {
@Override
public void AllChecked(boolean ischeck) {
if (!ischeck) {
cb_all.setChecked(false);
cb_alldel.setChecked(false);
} else {
cb_all.setChecked(true);
cb_alldel.setChecked(true);
}
}
});
carShopAdapter.setNumberAndPrice(new NumberAndPrice() {
@Override
public void AllNumAndPriceClick(final ArrayList<List<Car_child_shop_Bean>> list_shop_child) {
int max = 0;
int nums = 0;
for (List<Car_child_shop_Bean> list_child : list_shop_child) {
for (int i = 0; i < list_child.size(); i++) {
boolean childCheck = list_child.get(i).isChildCheck();
Car_child_shop_Bean car_child_shop_bean = list_child.get(i);
if (childCheck) {
int num = car_child_shop_bean.getNum();
double price = car_child_shop_bean.getPrice();
int prices = (int) price;
max += prices * num;
nums++;
}
}
}
tv_rental.setText("合計:¥" + max);
tv_num.setText("數量:" + nums);
}
});
pt_lv.setAdapter(carShopAdapter);
//讓他預設收回
int count = pt_lv.getCount();
for (int i = 0; i < count; i++) {
pt_lv.expandGroup(i);
}
pt_lv.setGroupIndicator(null);
//全選點選事件
cb_alldel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = cb_alldel.isChecked();
for (int i = 0; i < list_shop.size(); i++) {
list_shop.get(i).setFlag(checked);
}
for (List<Car_child_shop_Bean> i : list_shop_chiled) {
for (int j = 0; j < i.size(); j++) {
i.get(j).setChildCheck(checked);
}
}
carShopAdapter.notifyDataSetChanged();
}
});
carShopAdapter.setCarShop(new CarShopCheckedInterface() {
@Override
public void AllChecked(boolean ischeck) {
if (!ischeck) {
cb_all.setChecked(false);
cb_alldel.setChecked(false);
} else {
cb_all.setChecked(true);
cb_alldel.setChecked(true);
}
}
});
carShopAdapter.setNumberAndPrice(new NumberAndPrice() {
@Override
public void AllNumAndPriceClick(final ArrayList<List<Car_child_shop_Bean>> list_shop_child) {
int max = 0;
int nums = 0;
for (List<Car_child_shop_Bean> list_child : list_shop_child) {
for (int i = 0; i < list_child.size(); i++) {
boolean childCheck = list_child.get(i).isChildCheck();
Car_child_shop_Bean car_child_shop_bean = list_child.get(i);
if (childCheck) {
int num = car_child_shop_bean.getNum();
double price = car_child_shop_bean.getPrice();
int prices = (int) price;
max += prices * num;
nums++;
}
}
}
tv_rental.setText("合計:¥" + max);
tv_num.setText("數量:" + nums);
}
});
pt_lv.setAdapter(carShopAdapter);
//讓他預設收回
int count1 = pt_lv.getCount();
for (int i = 0; i < count1; i++) {
pt_lv.expandGroup(i);
}
pt_lv.setGroupIndicator(null);
}
}
}
//計算總價和數量s
public void foreach(ArrayList<List<Car_child_shop_Bean>> list_shop_child) {
carShopAdapter.notifyDataSetChanged();
int max = 0;
int nums = 0;
for (List<Car_child_shop_Bean> list_child : list_shop_child) {
for (int i = 0; i < list_child.size(); i++) {
boolean childCheck = list_child.get(i).isChildCheck();
Car_child_shop_Bean car_child_shop_bean = list_child.get(i);
if (childCheck) {
int num = car_child_shop_bean.getNum();
double price = car_child_shop_bean.getPrice();
int prices = (int) price;
max += prices * num;
nums++;
}
}
}
tv_rental.setText("合計:¥" + max);
tv_num.setText("數量:" + nums);
}
@Override
public void ShowOnFaildGwcv() {
}
@Override
public void ShowDelSuccess(DelBean delBean) {
}
@Override
public void ShowDelOnFaild() {
}
@Override
protected void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
@Override
public void onResume() {
super.onResume();
initSelCar();
}
}