购物车xml代码 主mainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rec"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/all"
android:layout_marginLeft="15dp"
/>
<TextView
android:layout_marginLeft="20dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="鎬昏锛?"
/>
<TextView
android:layout_marginLeft="15dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/num"
/>
<TextView
android:layout_marginLeft="15dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/pay"
/>
</LinearLayout>
</LinearLayout>
商家xml
<?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:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/shangjia"
android:layout_marginLeft="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/titles"
android:layout_marginLeft="15dp"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bac"
android:id="@+id/adapter_rec"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
商品xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<LinearLayout
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkboxitem"
android:layout_width="50dp"
android:layout_height="50dp" />
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@mipmap/ic_launcher_round"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="250dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:textColor="#FFED2318"
android:textSize="16sp"
android:id="@+id/price"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_width="60dp"
android:layout_height="wrap_content"
/>
<com.bwei.lenovo.jgouwuche.bar.ZiBar
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:rightBtn="@mipmap/jian"
app:leftBtn="@mipmap/jia"
android:id="@+id/barr">
</com.bwei.lenovo.jgouwuche.bar.ZiBar>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="25dp"/>
</LinearLayout>
drawable文件editss.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape>
<solid android:color="#FF6298C4"/>
<corners android:radius="15dp"/>
</shape>
</item>
<item android:state_pressed="true">
<shape>
<corners android:radius="15dp"/>
</shape>
</item>
</selector>
标题bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/insert"
android:background="@mipmap/jia"
/>
<TextView
android:gravity="center"
android:layout_marginLeft="30dp"
android:layout_width="70dp"
android:layout_height="30dp"
android:id="@+id/shuu"
android:background="@drawable/editss"
/>
<ImageView
android:layout_marginLeft="100dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/jian"
android:background="@mipmap/jian"
/>
</RelativeLayout>
Vlues写入attrs
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ZiBar">
<attr name="leftBtn" format="reference"/>
<attr name="centerText" format="string"/>
<attr name="rightBtn" format="reference"/>
</declare-styleable>
</resources>
购物车molder层接口
package com.bwei.lenovo.jgouwuche.model;
public interface ICarModel {
public void getCarModel(String url, CarCallBack carCallBack);
interface CarCallBack
{
void Success(Object obj);
void Fail();
}
}
购物车molder层类
package com.bwei.lenovo.jgouwuche.model;
import com.bwei.lenovo.jgouwuche.bean.CarBean;
import com.bwei.lenovo.jgouwuche.network.OkHttp;
public class CarModelImpl implements ICarModel {
@Override
public void getCarModel(String url, final CarCallBack carCallBack) {
OkHttp.getInstance().doGet(url, CarBean.class, new OkHttp.NetCallBack() {
@Override
public void LoadSuccess(Object obj) {
carCallBack.Success(obj);
}
@Override
public void LoadFail() {
carCallBack.Fail();
}
});
}
}
购物车presenter层接口
package com.bwei.lenovo.jgouwuche.presenter;
public interface ICarpresenter {
public void getCarPresenter();
}
购物车presenter层类
package com.bwei.lenovo.jgouwuche.presenter;
import com.bwei.lenovo.jgouwuche.MainActivity;
import com.bwei.lenovo.jgouwuche.api.Api;
import com.bwei.lenovo.jgouwuche.model.CarModelImpl;
import com.bwei.lenovo.jgouwuche.model.ICarModel;
public class CarPresenterImpl implements ICarpresenter {
MainActivity mainActivity;
private final CarModelImpl carModel;
public CarPresenterImpl(MainActivity mainActivity)
{
this.mainActivity =mainActivity ;
carModel = new CarModelImpl();
}
@Override
public void getCarPresenter() {
carModel.getCarModel(Api.CAS, new ICarModel.CarCallBack() {
@Override
public void Success(Object obj) {
mainActivity.getData(obj);
}
@Override
public void Fail() {
}
});
}
}
购物车商家adapter
package com.bwei.lenovo.jgouwuche.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.List;
import com.bwei.lenovo.jgouwuche.MainActivity;
import com.bwei.lenovo.jgouwuche.R;
import com.bwei.lenovo.jgouwuche.bean.CarBean;
public class CarAdapter extends RecyclerView.Adapter<CarAdapter.ViewHolder> {
Context context; CarBean carBean;
public CarAdapter(Context context, CarBean carBean) {
this.context = context;
this.carBean =carBean;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.car, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
String sellerName = carBean.getData().get(position).getSellerName();
Log.e("意义我一一一一班",sellerName +"");
holder.name.setText(sellerName);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
holder.adapter_rec.setLayoutManager(linearLayoutManager);
List<CarBean.DataBean.ListBean> list = carBean.getData().get(position).getList();
final ItemAdapter itemAdapter = new ItemAdapter(context, list);
holder.adapter_rec.setAdapter(itemAdapter);
//判断商家是不是被选中,如果选中
holder.shangjia.setChecked(carBean.getData().get(position).isChecked());
//子布局适配器点击事件
itemAdapter.setListener(new ItemAdapter.ShopCallBackListener() {
@Override
public void callBack() {
if(mShopCallBackListener !=null) {
mShopCallBackListener.callBack(carBean.getData());
}
List<CarBean.DataBean.ListBean> product = carBean.getData().get(position).getList();
boolean isAllChecked =true;
for (CarBean.DataBean.ListBean bean : product) {
if (!bean.isChecked()) {
//只要有一个商品未选中,标志位设置成false,并且跳出循环
isAllChecked = false;
break;
}
}
//刷新商家的状态
holder.shangjia.setChecked(isAllChecked);
carBean.getData().get(position).setChecked(isAllChecked);
}
});
holder.shangjia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
carBean.getData().get(position).setChecked(holder.shangjia.isChecked());
itemAdapter.selectOrremoveAll(holder.shangjia.isChecked());
}
});
}
@Override
public int getItemCount() {
return carBean.getData().size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
RecyclerView adapter_rec;
CheckBox shangjia;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.titles);
adapter_rec = itemView.findViewById(R.id.adapter_rec);
shangjia = itemView.findViewById(R.id.shangjia);
}
}
private ShopCallBackListener mShopCallBackListener;
public void setListener(ShopCallBackListener listener) {
this.mShopCallBackListener = listener;
}
public interface ShopCallBackListener {
void callBack(List<CarBean.DataBean> list);
}
}
购物车商品adapter
package com.bwei.lenovo.jgouwuche.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bwei.lenovo.jgouwuche.R;
import com.bwei.lenovo.jgouwuche.bar.ZiBar;
import com.bwei.lenovo.jgouwuche.bean.CarBean;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
Context context;
List<CarBean.DataBean.ListBean> list;
ItemAdapter itemAdapter;
public ItemAdapter(Context context, List<CarBean.DataBean.ListBean> list)
{
this.context =context;
this.list =list;
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_car, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position)
{
holder.name.setText(list.get(position).getTitle());
holder.price.setText(list.get(position).getBargainPrice());
Glide.with(context)
.load(list.get(position).getImages().split("\\|")[0].replace("https", "http"))
.into(holder.img);
//获得布局的商品
holder.barr.setData(context,list,position);
//判断商品是否选中
holder.checkBoxItem.setChecked(list.get(position).isChecked());
holder.checkBoxItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//如果备选中
list.get(position).setChecked(isChecked);
if(mShopCallBackListener != null)
{
mShopCallBackListener.callBack();
}
}
});
holder.barr.ClickListener(new ZiBar.ClickListener() {
@Override
public void Callback() {
if(mShopCallBackListener != null)
{
mShopCallBackListener.callBack();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView name,price;
ImageView img;
com.bwei.lenovo.jgouwuche.bar.ZiBar barr;
CheckBox checkBoxItem;
public ItemViewHolder(View itemView) {
super(itemView);
name= itemView.findViewById(R.id.name);
price= itemView.findViewById(R.id.price);
img = itemView.findViewById(R.id.img);
barr = itemView.findViewById(R.id.barr);
checkBoxItem = itemView.findViewById(R.id.checkboxitem);
}
}
//子产品的定义一个全选反选的方法
public void selectOrremoveAll(boolean isChecked)
{
for(CarBean.DataBean.ListBean listBean:list)
{
listBean.setChecked(isChecked);
}
notifyDataSetChanged();
}
//接口回调
private ShopCallBackListener mShopCallBackListener;
public void setListener(ShopCallBackListener listener) {
this.mShopCallBackListener = listener;
}
public interface ShopCallBackListener {
void callBack();
}
}
购物车bar类
package com.bwei.lenovo.jgouwuche.bar;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bwei.lenovo.jgouwuche.R;
import com.bwei.lenovo.jgouwuche.bean.CarBean;
import java.util.List;
public class ZiBar extends RelativeLayout {
ImageView insert;
ImageView jian;
TextView shuu;
public int postiotn;
Context context;
List<CarBean.DataBean.ListBean> list;
public ZiBar(final Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.bar,this);
insert= findViewById(R.id.insert);
shuu= findViewById(R.id.shuu);
jian =findViewById(R.id.jian);
//加
insert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String linag = list.get(postiotn).getNum();
int num = Integer.parseInt(linag.toString());
num++;
shuu.setText(num+"");
list.get(postiotn).setNum(num+"");
//回调接口
listener.Callback();
}
});
//减
jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String linag = list.get(postiotn).getNum();
int num = Integer.parseInt(linag.toString());
if(num >1)
{
num--;
}
else
{
Toast.makeText(context,"宝贝不能再少了",Toast.LENGTH_SHORT).show();
return;
}
shuu.setText(num+"");
list.get(postiotn).setNum(num+"");
//回调接口
listener.Callback();
}
});
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ZiBar);
int leftBtn = typedArray.getResourceId(R.styleable.ZiBar_leftBtn, 0);
int rightBtn = typedArray.getResourceId(R.styleable.ZiBar_rightBtn, 0);
String centerText = typedArray.getString(R.styleable.ZiBar_centerText);
typedArray.recycle();
insert.setBackgroundResource(leftBtn);
jian.setBackgroundResource(rightBtn);
shuu.setText(centerText);
}
//接口回调
public interface ClickListener
{
void Callback();
}
ClickListener listener ;
//实现接口
public void ClickListener(ClickListener listener)
{
this.listener = listener ;
}
//设置数量
public void setData(Context context, List<CarBean.DataBean.ListBean> list, int i)
{
this.context = context;
this.list =list;
postiotn = i;
shuu.setText(list.get(postiotn).getNum());
}
}
购物车主页面
package com.bwei.lenovo.jgouwuche;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.bwei.lenovo.jgouwuche.adapter.CarAdapter;
import com.bwei.lenovo.jgouwuche.bean.CarBean;
import com.bwei.lenovo.jgouwuche.presenter.CarPresenterImpl;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rec;
private CarPresenterImpl carPresenter;
com.bwei.lenovo.jgouwuche.bar.ZiBar bar;
private EditText shu;
private CarAdapter carAdapter;
private CheckBox all;
private CarBean carBean;
private TextView count;
private TextView pay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到商家的recycleview 的布局
rec = findViewById(R.id.rec);
// //数量
pay = findViewById(R.id.pay);
//价钱
count = findViewById(R.id.num);
//全选
all = findViewById(R.id.all);
//布局选择器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//设置布局方向
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
rec.setLayoutManager(linearLayoutManager);
//初始化presenter层
carPresenter = new CarPresenterImpl(this);
carPresenter.getCarPresenter();
//全选点击事件
all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkSeller(all.isChecked());
//刷新适配器
carAdapter.notifyDataSetChanged();
}
});
}
public void getData(Object data)
{
if( data instanceof CarBean)
{
carBean = (CarBean) data;
Log.e("啦啦啦啦啦啦啦",data+"");
carAdapter = new CarAdapter(MainActivity.this, carBean);
rec.setAdapter(carAdapter);
carAdapter.setListener(new CarAdapter.ShopCallBackListener() {
@Override
public void callBack(List<CarBean.DataBean> list) {
double totalPrice = 0;
int num =0;
int totalNum= 0;
for(int a=0 ;a<list.size();a++)
{
List<CarBean.DataBean.ListBean> listAll = list.get(a).getList();
for ( int i =0; i<listAll.size() ; i++)
{
totalNum = totalNum +Integer.parseInt(listAll.get(i).getNum());
if(listAll.get(i).isChecked())
{
totalPrice = totalPrice + (Double.valueOf(listAll.get(i).getBargainPrice())*Double.valueOf(listAll.get(i).getNum()));
num = num +Integer.parseInt(listAll.get(i).getNum());
}
}
if(totalNum>num)
{
all.setChecked(false);
}else{
all.setChecked(true);
}
count.setText(totalPrice+"");
pay.setText("去结算(" + num + ")");
}
}
});
}
}
//全选反选时控制价格,和数量
private void checkSeller(boolean bool) {
double totalPrice = 0;
int num = 0;
//循环遍历商家,看商家是否选中
for (int a = 0; a < carBean.getData().size(); a++) {
//遍历商家,改变状态
CarBean.DataBean dataBean = carBean.getData().get(a);
dataBean.setChecked(bool);
//循环遍历产品,看产品是否选中
List<CarBean.DataBean.ListBean> listAll = carBean.getData().get(a).getList();
for (int i = 0; i < listAll.size(); i++) {
//遍历商品,改变状态 如果选中了
listAll.get(i).setChecked(bool);
//计算价格数量
totalPrice = totalPrice + (Double.valueOf(listAll.get(i).getPrice() )* Double.valueOf(listAll.get(i).getNum()));
num = num +Integer.parseInt(listAll.get(i).getNum());
}
}
if (bool) {
//给价格数量赋值
count.setText(totalPrice+"");
pay.setText("去结算(" + num + ")");
} else {
count.setText("0.00");
pay.setText("去结算(0)");
}
}
}