具体代码:https://github.com/sunli0828/Month_Demo/tree/master/sunli1218
购物车接口:
public static final String URL_SHOPCAR_INFO = “****************************”;
购物车主页面
public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
private IPresenterImpl iPresenter;
private RecyclerView main_recycler_car;
private CheckBox main_ck_all;
private TextView main_text_totalPrice;
private Button main_btn_totalNum;
private List carBeanData;
private SellerAdapter sellerAdapter;
private Button main_btn_linkage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iPresenter = new IPresenterImpl(this);
iPresenter.startRequest(ApiUtils.URL_SHOPCAR_INFO, null, CarBean.class);
initView();
}
//加载视图
private void initView() {
main_recycler_car = findViewById(R.id.main_recycler_car);
main_ck_all = findViewById(R.id.main_ck_all);
main_text_totalPrice = findViewById(R.id.main_text_totalPrice);
main_btn_totalNum = findViewById(R.id.main_btn_totalNum);
main_btn_linkage = findViewById(R.id.main_btn_linkage);
main_ck_all.setOnClickListener(this);
main_btn_totalNum.setOnClickListener(this);
main_btn_linkage.setOnClickListener(this);
//第一层商家的适配器
sellerAdapter = new SellerAdapter(this);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
main_recycler_car.setLayoutManager(linearLayoutManager);
main_recycler_car.setAdapter(sellerAdapter);
sellerAdapter.setListener(new SellerAdapter.CarCallBackListener() {
@Override
public void callBack(List list) {
double totalPrice = 0;
int num = 0, totalNum = 0;
for (int a = 0; a < list.size(); a++) {
List listSellerThings = list.get(a).getList();
for (int i = 0; i < listSellerThings.size(); i++) {
totalNum += listSellerThings.get(i).getNum();
if (listSellerThings.get(i).isCheck()) {
totalPrice += listSellerThings.get(i).getPrice() * listSellerThings.get(i).getNum();
num += listSellerThings.get(i).getNum();
}
}
}
if (num < totalNum) {
main_ck_all.setChecked(false);
} else {
main_ck_all.setChecked(true);
}
main_text_totalPrice.setText("合计: ¥" + totalPrice);
main_btn_totalNum.setText("付款(" + num + ")");
}
});
}
//按钮点击事件的方法
@Override
public void onClick(View v) {
switch (v.getId()) {
//全选
case R.id.main_ck_all:
boolean checked = main_ck_all.isChecked();
checkSeller(checked);
break;
//总数
case R.id.main_btn_totalNum:
Intent intentMap = new Intent(MainActivity.this, MapActivity.class);
startActivity(intentMap);
break;
//这是二级联动
case R.id.main_btn_linkage:
Intent intentLevel = new Intent(MainActivity.this, LevelLinkActivity.class);
startActivity(intentLevel);
break;
default:
break;
}
}
private void checkSeller(boolean bool) {
double totalPrice = 0;
int num = 0;
for (int a = 0; a < carBeanData.size(); a++) {
CarBean.DataBean dataBean = carBeanData.get(a);
dataBean.setCheck(bool);
List listSellerThings = carBeanData.get(a).getList();
for (int i = 0; i < listSellerThings.size(); i++) {
listSellerThings.get(i).setCheck(bool);
totalPrice = totalPrice + (listSellerThings.get(i).getPrice() * listSellerThings.get(i).getNum());
num = num + listSellerThings.get(i).getNum();
}
}
if (bool) {
main_text_totalPrice.setText("合计: " + totalPrice);
main_btn_totalNum.setText("付款(" + num + ")");
} else {
main_text_totalPrice.setText("合计: 0.00");
main_btn_totalNum.setText("付款(0)");
}
sellerAdapter.notifyDataSetChanged();
}
//mvp请求数据成功回调方法
@Override
public void showResponseData(Object data) {
if (data instanceof CarBean) {
CarBean carBean = (CarBean) data;
carBeanData = carBean.getData();
if (carBeanData != null) {
carBeanData.remove(0);
sellerAdapter.setList(carBeanData);
}
}
}
@Override
public void showResponseFail(Exception e) {}
@Override
protected void onDestroy() {
super.onDestroy();
iPresenter.onDetach();
}
}
第一层 商家的名字的适配器
public class SellerAdapter extends RecyclerView.Adapter {
private List dataBeanList = new ArrayList<>();
private Context dataContext;
public SellerAdapter(Context dataContext) {
this.dataContext = dataContext;
}
//创建视图
@NonNull
@Override
public SellerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(dataContext, R.layout.adapter_item_seller, null);
SellerViewHolder sellerViewHolder = new SellerViewHolder(view);
return sellerViewHolder;
}
//绑定视图
@Override
public void onBindViewHolder(@NonNull final SellerViewHolder sellViewHolder, final int i) {
sellViewHolder.seller_name.setText(dataBeanList.get(i).getSellerName());
//商家商品内容的适配器
final SellerThingsAdapter sellerThingsAdapter = new SellerThingsAdapter(dataBeanList.get(i).getList(), dataContext);
//线性管理
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(dataContext);
sellViewHolder.seller_recycler.setLayoutManager(linearLayoutManager);
//设置适配器
sellViewHolder.seller_recycler.setAdapter(sellerThingsAdapter);
sellViewHolder.seller_check.setChecked(dataBeanList.get(i).isCheck());
sellerThingsAdapter.setListener(new SellerThingsAdapter.CarCallBackListener() {
@Override
public void callBack() {
if (carCallBackListener != null) {
carCallBackListener.callBack(dataBeanList);
}
List listBeans = dataBeanList.get(i).getList();
boolean isAllChecked = true;
for (CarBean.DataBean.ListBean bean : listBeans) {
if (!bean.isCheck()) {
isAllChecked = false;
break;
}
}
sellViewHolder.seller_check.setChecked(isAllChecked);
dataBeanList.get(i).setCheck(isAllChecked);
}
});
sellViewHolder.seller_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataBeanList.get(i).setCheck(sellViewHolder.seller_check.isChecked());
sellerThingsAdapter.selectOrRemoveAll(sellViewHolder.seller_check.isChecked());
}
});
}
@Override
public int getItemCount() {
return dataBeanList.size();
}
//内部类
public class SellerViewHolder extends RecyclerView.ViewHolder {
RecyclerView seller_recycler;
TextView seller_name;
CheckBox seller_check;
public SellerViewHolder(@NonNull View itemView) {
super(itemView);
seller_recycler = itemView.findViewById(R.id.seller_recycler);
seller_name = itemView.findViewById(R.id.item_text_sellerName);
seller_check = itemView.findViewById(R.id.checked_seller);
}
}
public void setList(List list) {
this.dataBeanList = list;
notifyDataSetChanged();
}
//接口回调
private CarCallBackListener carCallBackListener;
public void setListener(CarCallBackListener listener) {
this.carCallBackListener = listener;
}
public interface CarCallBackListener {
void callBack(List list);
}
}
//商品的适配器
public class SellerThingsAdapter extends RecyclerView.Adapter {
private List listBeanList = new ArrayList<>();
private Context listBeanContext;
public SellerThingsAdapter(List listBeanList, Context listBeanContext) {
this.listBeanList = listBeanList;
this.listBeanContext = listBeanContext;
}
@NonNull
@Override
public SellerTingsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(listBeanContext, R.layout.adapter_item_seller_things, null);
SellerTingsViewHolder sellerTingsViewHolder = new SellerTingsViewHolder(view);
return sellerTingsViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final SellerTingsViewHolder sellerTingsViewHolder, final int i) {
String url = listBeanList.get(i).getImages().split("\\|")[0].replace("https", "http");
Glide.with(listBeanContext).load(url).into(sellerTingsViewHolder.sellerthings_icon);
sellerTingsViewHolder.sellerthings_title.setText(listBeanList.get(i).getTitle());
sellerTingsViewHolder.sellerthings_price.setText(listBeanList.get(i).getPrice() + "");
sellerTingsViewHolder.sellerthings_ckAll.setChecked(listBeanList.get(i).isCheck());
sellerTingsViewHolder.sellerthings_ckAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listBeanList.get(i).setCheck(isChecked);
if (carCallBackListener != null) {
carCallBackListener.callBack();
}
}
});
}
@Override
public int getItemCount() {
return listBeanList.size();
}
public class SellerTingsViewHolder extends RecyclerView.ViewHolder{
CheckBox sellerthings_ckAll;
ImageView sellerthings_icon;
TextView sellerthings_title;
TextView sellerthings_price;
public SellerTingsViewHolder(@NonNull View itemView) {
super(itemView);
sellerthings_ckAll = itemView.findViewById(R.id.item_things_check_product);
sellerthings_icon = itemView.findViewById(R.id.item_things_icon_product);
sellerthings_title = itemView.findViewById(R.id.item_things_text_product_title);
sellerthings_price = itemView.findViewById(R.id.item_things_text_product_price);
}
}
public void selectOrRemoveAll(boolean isSelectedAll) {
for (CarBean.DataBean.ListBean listBean : listBeanList) {
listBean.setCheck(isSelectedAll);
}
notifyDataSetChanged();
}
private CarCallBackListener carCallBackListener;
public void setListener(CarCallBackListener listener) {
this.carCallBackListener = listener;
}
public interface CarCallBackListener {
void callBack();
}
}
自定义View做加减
//xml文件
① activity_main.xml
② adapter_item_seller.xml
③ adapter_item_seller_things
④ 自定义view