分享一个 功能简单实用 ,实际操作起来 不算太难 ,我这里 是写在fragment中,跟Activity里的实现 没有太大的区别
先跟大家说一下 购物车 简单的 逻辑
首先在布局文件中 创建 一个 底部视图 ,视图中 包含全选按钮,总价text,还有 结算按钮 即可
其次 在上面 写RecyclerView 或者 你们 习惯的 ListView…(我在这里 用了一个SmartRefreshLayout 是一个 第三方的刷新 )
然后 我们要知道 购物车 结构 是 嵌套的 也是 所谓的 二级联动 ,什么意思? 意思就是在 一个 RecyclerView 的子布局 中 在写 一个 RecyclerView ,在最外层的 适配器 里 找到 子布局中的 RecyclerView , 并对它进行 实现展示条目操作 ,其余 就不多说了 ,逻辑的实现在代码中也都有体现 ,大家继续往下看 就好
大家所需要配置的 依赖
//OKGO
implementation 'com.lzy.net:okgo:3.0.4'
implementation 'com.google.code.gson:gson:2.8.5'
//RecyclerView简单方法
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//Glide加载图片
implementation 'com.github.bumptech.glide:glide:3.7.0'
//butterknife快速查找资源的依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//gson解析的依赖
implementation 'com.google.code.gson:gson:2.8.2'
//导入Design包
implementation 'com.android.support:design:28.+'
//SmartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-19'
购物车 主页 Activity或 fragment 对应的 xml 布局文件
布局文件 对应的 fragment 或 Activity
public class oneFrag extends Fragment implements IContract.IView, View.OnClickListener {
private RecyclerView recylerlist;
private Presenter presenter;
private CheckBox cb_all;
private List data;
private MyBusinessAdapter myOneAdapter;
private TextView tv_count;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.onefrag,container,false);
//获取资源id
recylerlist = view.findViewById(R.id.one_recylerView);
cb_all = view.findViewById(R.id.cb_all);
tv_count = view.findViewById(R.id.tv_count);
//创建P层
presenter = new Presenter();
presenter.attachView(this);
presenter.requestData();
return view;
}
@Override
public void showData(String responseData) {
Toast.makeText(getActivity(), responseData, Toast.LENGTH_SHORT).show();
//全选按钮的选中监听
cb_all.setOnCheckedChangeListener(null);
//全选的点击事件
cb_all.setOnClickListener(this);
//解析 Gson 并用 RecyclerView 展示 (我这里使用的是 简单版的 RecyclerView ,上面 有 第三方依赖)
Gson gson = new Gson ();
ShoppingCartBean shoppingCartBean = gson.fromJson(responseData, ShoppingCartBean.class);
data = shoppingCartBean.getData();
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recylerlist.setLayoutManager(manager);
myOneAdapter = new MyBusinessAdapter(R.layout.oneitem_business, data);
recylerlist.setAdapter(myOneAdapter);
myOneAdapter.notifyDataSetChanged();
//接口 回调 适配器 中的接口回调 方法
myOneAdapter.setOnBusinessItemClickLisenter(new MyBusinessAdapter.OnBusinessItemClickLisenter() {
@Override
public void onCallBack() {
boolean result =true;
for (int i = 0; i < data.size(); i++) {
boolean businessChecked = data.get(i).getBusinessChecked();
result = result & businessChecked;
//里层状态
for (int j = 0; j < data.get(i).getList().size(); j++) {
boolean goodsChecked = data.get(i).getList().get(j).getGoodsChecked();
result =result & goodsChecked;
}
}
cb_all.setChecked(result);
//计算总价
calculateTotalCount();
}
});
}
//计算总价
private void calculateTotalCount() {
//对总价进行计算
double totalCount = 0;
//外层条目
for (int i = 0; i < data.size(); i++) {
//内层条目
for (int j = 0; j < data.get(i).getList().size(); j++) {
if (data.get(i).getList().get(j).getGoodsChecked() == true){
double price = data.get(i).getList().get(j).getPrice();
int defalutNumber = data.get(i).getList().get(j).getDefalutNumber();
double goodsPrice = price * defalutNumber;
totalCount =totalCount+goodsPrice;
}
}
}
tv_count.setText("总价是:" +String.valueOf(totalCount));
}
@Override
public void onDestroy() {
super.onDestroy();
presenter.deatchView(this);
}
@Override
//点击全选按钮
public void onClick(View v) {
//外层
for (int i = 0; i < data.size(); i++) {
data.get(i).setBusinessChecked(cb_all.isChecked());
//内层
for (int j = 0; j < data.get(i).getList().size(); j++) {
data.get(i).getList().get(j).setGoodsChecked(cb_all.isChecked());
}
}
myOneAdapter.notifyDataSetChanged();
calculateTotalCount();
}
}
MyBusinessAdapter适配器
public class MyBusinessAdapter extends BaseQuickAdapter {
//接口回调
private OnBusinessItemClickLisenter onBusinessItemClickLisenter;
public interface OnBusinessItemClickLisenter{
public void onCallBack();
}
public void setOnBusinessItemClickLisenter(OnBusinessItemClickLisenter onBusinessItemClickLisenter){
this.onBusinessItemClickLisenter =onBusinessItemClickLisenter;
}
public MyBusinessAdapter(int layoutResId, @Nullable List data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShoppingCartBean.DataBean item) {
helper.setText(R.id.tv_business_name,item.getSellerName());
//加载布局
RecyclerView rv_goods = helper.getView(R.id.rv_goods);
List list = item.getList();
LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
rv_goods.setLayoutManager(manager);
final MyGoodsAdapter myGoodsAdapter = new MyGoodsAdapter(R.layout.oneitem_goods,list);
rv_goods.setAdapter(myGoodsAdapter);
final CheckBox cb_business = helper.getView(R.id.cb_business);
//获取商家条目是否选中状态
cb_business.setChecked(item.getBusinessChecked());
//首先处理内部子条目控制外部条目,接口回调最内层适配器中的 接口回调方法
myGoodsAdapter.setOnGoodsItemClickLisenter(new MyGoodsAdapter.OnGoodsItemClickLisenter() {
@Override
public void onCallBack() {
//遍历获取所有子条目,只要有一个未勾选,商品类别也应该是未勾选
boolean result = true;
for (int i = 0; i < item.getList().size(); i++) {
result =result&item.getList().get(i).getGoodsChecked();
}
cb_business.setChecked(result);
myGoodsAdapter.notifyDataSetChanged();
//把最后的状态进行回传
onBusinessItemClickLisenter.onCallBack();
}
});
//然后外层的商品类别条目需要控制里面的商品条目
cb_business.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取商品类别勾选状态
//外层商品类别条目获取的关键:cb_business.isChecked()
for (int i = 0; i < item.getList().size(); i++) {
item.getList().get(i).setGoodsChecked(cb_business.isChecked());
}
item.setBusinessChecked(cb_business.isChecked());
notifyDataSetChanged();
//把最后的状态进行回传
onBusinessItemClickLisenter.onCallBack();
}
});
myGoodsAdapter.notifyDataSetChanged();
}
首页 RecyclerView所 对应的 布局
//看到没 就是 在 RecyclerView中的子布局 里 在写上一个 RecyclerView
MyBusinessAdapter适配器中 创建的 MyGoodsAdapter 适配器
class MyGoodsAdapter extends BaseQuickAdapter {
//接口回调
OnGoodsItemClickLisenter onGoodsItemClickLisenter;
public interface OnGoodsItemClickLisenter{
public void onCallBack();
}
public void setOnGoodsItemClickLisenter(OnGoodsItemClickLisenter onGoodsItemClickLisenter){
this.onGoodsItemClickLisenter=onGoodsItemClickLisenter;
}
public MyGoodsAdapter(int layoutResId, @Nullable List data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShoppingCartBean.DataBean.ListBean item) {
helper.setText(R.id.tv_goodsTitle,item.getTitle());
helper.setText(R.id.tv_goodsPrice,"¥"+item.getPrice());
//给 布局控件 进行 赋值
ImageView image = helper.getView(R.id.iv_goodsIcon);
String images = item.getImages();
String[] split = images.split("\\|");
Glide.with(mContext).load(split[0]).into(image);
//对 子布局中的多选框 进行 操作
CheckBox cb_goods = helper.getView(R.id.cb_goods);
cb_goods.setOnCheckedChangeListener(null);
cb_goods.setChecked(item.getGoodsChecked());
//选中 监听
cb_goods.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Bean对象更新
item.setGoodsChecked(isChecked);
onGoodsItemClickLisenter.onCallBack();
}
});
//加减器
CalculatorView calculatorView = helper.getView(R.id.cv_calculatorView);
//调用Class中的接口
calculatorView.setOnCalculatorLisenter(new CalculatorView.OnCalculatorLisenter() {
@Override
public void onDecrese(int number) {
//对新增的字段进行改动
item.setDefalutNumber(number);
onGoodsItemClickLisenter.onCallBack();
}
@Override
public void onAdd(int number) {
item.setDefalutNumber(number);
onGoodsItemClickLisenter.onCallBack();
}
});
}
}
子布局中的 RecyclerView 所对应的 布局 xml
//这个是 自定义 View 的 class 引用
**自建Class文件, 主要是 负责处理 加减问题 **
public class CalculatorView extends LinearLayout implements View.OnClickListener {
private final TextView tv_number;
private final Button btn_decrease;
private final Button btn_add;
public CalculatorView(Context context, AttributeSet attrs) {
super(context, attrs);
//填充的条目布局
View view =LayoutInflater.from(context).inflate(R.layout.calculator_layout,this);
//获取资源id
btn_add = view.findViewById(R.id.btn_add);
btn_decrease = view.findViewById(R.id.btn_decrease);
tv_number = view.findViewById(R.id.tv_number);
//实现 点击 监听
btn_add.setOnClickListener(this);
btn_decrease.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String numberString = tv_number.getText().toString();
int number = Integer.parseInt(numberString);
switch (v.getId()){
case R.id.btn_decrease:
number = number -1;
if (number < 0){
number = 0;
//最小为0
tv_number.setText(String.valueOf(number));
}
tv_number.setText(String.valueOf(number));
//接口回调回传数字
onCalculatorLisenter.onDecrese(number);
break;
case R.id.btn_add:
number = number+1;
tv_number.setText(String.valueOf(number));
//接口回调回传数字
onCalculatorLisenter.onAdd(number);
break;
}
}
OnCalculatorLisenter onCalculatorLisenter;
public interface OnCalculatorLisenter{
//减少
public void onDecrese(int number);
//增加
public void onAdd(int number);
}
public void setOnCalculatorLisenter(OnCalculatorLisenter onCalculatorLisenter){
this.onCalculatorLisenter = onCalculatorLisenter;
}
}
加减器布局