这一篇废话比较少一些,直接上代码!样式布局在下面!
1.购物车解析数据及其勾选逻辑
@Override
public void showData(String string) {
Gson gson = new Gson();
HomeBean homeBean = gson.fromJson(string, HomeBean.class);
//外层商家条目的数据源
data = homeBean.getData();
//删除第一个商家(空的)
data.remove(0);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
businessRcl.setLayoutManager(layoutManager);
badapter = new MybusinessAdapter(R.layout.presant, data);
businessRcl.setAdapter(badapter);
//避免焦点抢占
allCb.setOnCheckedChangeListener(null);
//全选的点击监听
allCb.setOnClickListener(this);
badapter.setOnBusinessItemClickLisenter(new MybusinessAdapter.OnBusinessItemClickLisenter() {
@Override
public void onCallBack() {
boolean result=true;
for (int i = 0; i < data.size(); i++) {
//外层选中状态
result=result & data.get(i).isBusinesscheck();
for (int j = 0; j < data.get(i).getList().size(); j++) {
//里层选中状态
result=result & data.get(i).getList().get(j).isGoodscheck();
}
}
allCb.setChecked(result);
//计算总价
zongjia();
}
});
private void zongjia() {
//对总价进行计算,在Bean包中定义一个private double DefalutNumber=1;加getset方法
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).isGoodscheck()==true){
double price = data.get(i).getList().get(j).getPrice();
//获取商品数据*商品价格
double defalutNumber = data.get(i).getList().get(j).getDefalutNumber();
double goodsPrice=price * defalutNumber;
totalCount=totalCount+goodsPrice;
}
}
}
allZj.setText("总价是:"+String.valueOf(totalCount));
}
//全选监听的点击,要在内外层商家的Bean中添加全选的方法,setget方法
@Override
public void onClick(View v) {
//全选逻辑的处理
for (int i = 0; i < data.size(); i++) {
//首先让外层的商家条目全部选中
data.get(i).setBusinesscheck(allCb.isChecked());
//然后让里层的商品条目全部选中
for (int j = 0; j < data.get(i).getList().size(); j++) {
data.get(i).getList().get(j).setGoodscheck(allCb.isChecked());
}
}
//适配器刷新
badapter.notifyDataSetChanged();
//计算总价
zongjia();
}
}
2.外层商家的适配器
//接口回调
OnBusinessItemClickLisenter onBusinessItemClickLisenter;
public void setOnBusinessItemClickLisenter(OnBusinessItemClickLisenter onBusinessItemClickLisenter) {
this.onBusinessItemClickLisenter = onBusinessItemClickLisenter;
}
public interface OnBusinessItemClickLisenter{
public void onCallBack();
}
//设置商家名称
helper.setText(R.id.business_name,item.getSellerName());
//外层商家条目的数据源
final List<HomeBean.DataBean.ListBean> goods_data = item.getList();
RecyclerView goodsrly = helper.getView(R.id.goods_rly);
LinearLayoutManager layoutManager=new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
goodsrly.setLayoutManager(layoutManager);
final MygoodsAdapter gadapter =new MygoodsAdapter(R.layout.goods, goods_data);
//设置子条目适配器
goodsrly.setAdapter(gadapter);
//避免焦点抢占
final CheckBox businesscb = helper.getView(R.id.business_cb);
businesscb.setOnCheckedChangeListener(null);
//获取商家条目是否选中状态
businesscb.setChecked(item.isBusinesscheck());
//首先处理内部子条目控制外部条目
gadapter.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).isGoodscheck();
}
businesscb.setChecked(result);
//刷新适配器
gadapter.notifyDataSetChanged();
//把最后的状态进行回传,上面需要写接口回调
onBusinessItemClickLisenter.onCallBack();
}
});
//然后外层的商品类别条目需要控制里面的商品条目
businesscb.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).setGoodscheck(businesscb.isChecked());
}
item.setBusinesscheck(businesscb.isChecked());
notifyDataSetChanged();
//把最后的状态进行回传
onBusinessItemClickLisenter.onCallBack();
}
});
}
}
3.里层商品适配器
//接口回调
OnGoodsItemClickLisenter onGoodsItemClickLisenter;
public void setOnGoodsItemClickLisenter(OnGoodsItemClickLisenter onGoodsItemClickLisenter) {
this.onGoodsItemClickLisenter = onGoodsItemClickLisenter;
}
public interface OnGoodsItemClickLisenter{
public void onCallBack();
}
//设置商品名称
helper.setText(R.id.goods_name,item.getTitle());
//设置商品图片
helper.setText(R.id.goods_jq,item.getPrice()+"");
String images = item.getImages();
//截取图片
String[] split = images.split("\\|");
Glide.with(mContext).load(split[0]).into((ImageView) helper.getView(R.id.goods_img));
//避免焦点抢占
final CheckBox goodscb = helper.getView(R.id.goods_cb);
goodscb.setOnCheckedChangeListener(null);
goodscb.setChecked(item.isGoodscheck());
//以接口的方式把状态回传给外层
goodscb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Bean对象状态进行更新完毕
item.setGoodscheck(isChecked);
onGoodsItemClickLisenter.onCallBack();
}
});
//加减器
classcalculator jjview = helper.getView(R.id.class_jjview);
jjview.setOnCalCulatorLisenter(new classcalculator.OnCalCulatorLisenter() {
@Override
public void onDecrese(int number) {
//对新增的字段进行改动
item.setDefalutNumber(number);
onGoodsItemClickLisenter.onCallBack();
}
@Override
public void onAdd(int number) {
//对新增的字段进行改动
item.setDefalutNumber(number);
onGoodsItemClickLisenter.onCallBack();
}
});
}
}
4.加减逻辑
自定义view一个加减类,在此类中进行加减逻辑并会调到activity中。
public classcalculator(Context context, AttributeSet attrs) {
super(context, attrs);
//填充的条目布局
View inflate = View.inflate(context, R.layout.class_calculator, null);
buttonadd = inflate.findViewById(R.id.button_add);
buttonreduce = inflate.findViewById(R.id.button_reduce);
textnumber = inflate.findViewById(R.id.text_number);
addView(inflate);
buttonadd.setOnClickListener(this);
buttonreduce.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String numberString = textnumber.getText().toString();
int number = Integer.parseInt(numberString);
switch (v.getId()){
case R.id.button_add:
number=number+1;
textnumber.setText(String.valueOf(number));
//接口回调回传数字
onCalCulatorLisenter.onAdd(number);
break;
case R.id.button_reduce:
number=number - 1;
if (number<0){
number=0;
//最小数量为0
textnumber.setText(String.valueOf(number));
}
textnumber.setText(String.valueOf(number));
//接口回调回传数字
onCalCulatorLisenter.onDecrese(number);
break;
}
}
//接口回调
OnCalCulatorLisenter onCalCulatorLisenter;
public void setOnCalCulatorLisenter(OnCalCulatorLisenter onCalCulatorLisenter) {
this.onCalCulatorLisenter = onCalCulatorLisenter;
}
public interface OnCalCulatorLisenter{
//减少
public void onDecrese(int number);
//增加
public void onAdd(int number);
}
5.全选及结算样式
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/business_rcl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ll">
</android.support.v7.widget.RecyclerView>
<RelativeLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
>
<CheckBox
android:id="@+id/all_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="全选" />
<TextView
android:id="@+id/all_zj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="185dp"
android:layout_centerVertical="true"
android:padding="10dp"
android:text="总计:0" />
<Button
android:id="@+id/all_js"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/mygouwucheline"
android:text="结算" />
</RelativeLayout>
</RelativeLayout>
6.外层商家布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="@drawable/homeshop"
android:layout_margin="5dp"
android:layout_marginTop="20dp"
android:paddingTop="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/business_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/business_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="lalala"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/goods_rly"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
7.里层商品布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="@drawable/homeshop"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_marginLeft="@dimen/dp_10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/goods_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<ImageView
android:id="@+id/goods_img"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="@dimen/dp_10"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_10"
android:orientation="vertical">
<TextView
android:id="@+id/goods_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="lalala"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/goods_jq"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ba0404"
android:textSize="15dp"
android:text="lalala"/>
<zhoukao.zjq.com.shopping.ui.classcalculator
android:id="@+id/class_jjview"
android:layout_marginTop="@dimen/dp_10"
android:layout_marginLeft="18dp"
android:layout_width="match_parent"
android:layout_height="30dp">
</zhoukao.zjq.com.shopping.ui.classcalculator>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
8.自定义view加减布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_reduce"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/reduce"/>
<TextView
android:id="@+id/text_number"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center"
android:inputType="number"
android:text="1" />
<Button
android:id="@+id/button_add"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/add"/>
</LinearLayout>
创作不易,仅供参考,如有问题,评论留言,如有雷同,纯属巧合!