购物车内部布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/iutter_ck"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iutter_iv"
android:layout_width="100dp"
android:layout_height="100dp"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:failureImage="@mipmap/ic_launcher_round"
fresco:roundedCornerRadius="30dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/iutter_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/iutter_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/iutter_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.example.administrator.yk.data.view.AddView
android:id="@+id/addview"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_marginTop="60dp"
android:layout_weight="1"
></com.example.administrator.yk.data.view.AddView>
</LinearLayout>
</LinearLayout>
购物车外部
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/outter_ck"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/outter_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/outter_recy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
购物车最大的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/shopp_recy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
></android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<CheckBox
android:id="@+id/quanxuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/t_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="结算"
android:textColor="#f00"
android:layout_weight="1" />
<Button
android:id="@+id/buy"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="结算" />
</LinearLayout>
</RelativeLayout>
加减器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<Button
android:id="@+id/but_add"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="+"
/>
<TextView
android:id="@+id/t_Num"
android:layout_width="50dp"
android:layout_height="50dp" />
<Button
android:id="@+id/but_jian"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="-"
/>
</LinearLayout>
内部适配器
public class IutterAdapater extends BaseQuickAdapter<ShoppBean.ResultBean.ShoppingCartListBean,BaseViewHolder> {
public IutterAdapater(int layoutResId, @Nullable List<ShoppBean.ResultBean.ShoppingCartListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShoppBean.ResultBean.ShoppingCartListBean item) {
final CheckBox iutter_ck = helper.getView(R.id.iutter_ck);
helper.setChecked(R.id.iutter_ck,item.isOnInnerchecked());
// TODO: 2019/10/26 调用
iutter_ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onInnerListener.onInnerchanged();
}
});
SimpleDraweeView iutter_iv = helper.getView(R.id.iutter_iv);
iutter_iv.setImageURI(item.getPic());
helper.setText(R.id.iutter_name,item.getCommodityName());
helper.setText(R.id.iutter_price,item.getPrice()+"");
helper.setText(R.id.iutter_num,item.getCount()+"");
// TODO: 2019/10/26 商品选中
iutter_ck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 2019/10/26 看商品是否是选中状态
boolean checked = iutter_ck.isChecked();
// TODO: 2019/10/26 如果商品未选中的时候去进行设置选中状态
item.setOnInnerchecked(checked);
// TODO: 2019/10/26 刷新
notifyDataSetChanged();
// TODO: 2019/10/26 刷新商品的 回调状态
onInnerListener.onInnerchanged();
}
});
AddView addview = helper.getView(R.id.addview);
addview.setText(item.getCount());
addview.setSetCount(new AddView.setCount() {
@Override
public void Count(int count) {
item.setCount(count);
onInnerListener.onInnerchanged();
}
});
}
public interface onInnerListener{
void onInnerchanged();
}
onInnerListener onInnerListener;
public IutterAdapater.onInnerListener getOnInnerListener() {
return onInnerListener;
}
public void setOnInnerListener(IutterAdapater.onInnerListener onInnerListener) {
this.onInnerListener = onInnerListener;
}
}
外部适配器
public class OutterAdapater extends BaseQuickAdapter<ShoppBean.ResultBean,BaseViewHolder> {
private IutterAdapater iutterAdapater;
public OutterAdapater(int layoutResId, @Nullable List<ShoppBean.ResultBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShoppBean.ResultBean item) {
final CheckBox outter_ck = helper.getView(R.id.outter_ck);
// TODO: 2019/10/26 全选
helper.setChecked(R.id.outter_ck,item.isOnOutterchecked());
outter_ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onOutterListener.onOutterChaned();
}
});
helper.setText(R.id.outter_name,item.getCategoryName());
// TODO: 2019/10/26 二级列表
RecyclerView outter_recy = helper.getView(R.id.outter_recy);
outter_recy.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
iutterAdapater = new IutterAdapater(R.layout.iutter, item.getShoppingCartList());
outter_recy.setAdapter(iutterAdapater);
// TODO: 2019/10/26 看商家是否选中
outter_ck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 2019/10/26 商家是否选中
boolean checked = outter_ck.isChecked();
// TODO: 2019/10/26 给商品设置选中状态
item.setOnOutterchecked(checked);
// TODO: 2019/10/26 循环商家下的商品
for (int i = 0; i <item.getShoppingCartList().size() ; i++) {
// TODO: 2019/10/26 给商品也设置选中状态
item.getShoppingCartList().get(i).setOnInnerchecked(checked);
onOutterListener.onOutterChaned();
notifyDataSetChanged();
iutterAdapater.notifyDataSetChanged();
}
}
});
iutterAdapater.setOnInnerListener(new IutterAdapater.onInnerListener() {
@Override
public void onInnerchanged() {
boolean innerAll=true;
for (int i = 0; i <item.getShoppingCartList().size() ; i++) {
// TODO: 2019/10/26 看商品是否选中状态
boolean onInnerchecked = item.getShoppingCartList().get(i).isOnInnerchecked();
innerAll=innerAll&onInnerchecked;
}
// TODO: 2019/10/26 给商家也去设置选中状态
item.setOnOutterchecked(innerAll);
onOutterListener.onOutterChaned();
iutterAdapater.notifyDataSetChanged();
notifyDataSetChanged();
}
});
}
public interface onOutterListener{
void onOutterChaned();
}
onOutterListener onOutterListener;
public OutterAdapater.onOutterListener getOnOutterListener() {
return onOutterListener;
}
public void setOnOutterListener(OutterAdapater.onOutterListener onOutterListener) {
this.onOutterListener = onOutterListener;
}
}
购物车Fragment或Activity页面
public class TabShoppingCart extends BaseFragment<ShoppContract.ShoppView, ShoppPersenter<ShoppContract.ShoppView>> implements ShoppContract.ShoppView {
@BindView(R.id.shopp_recy)
RecyclerView shoppRecy;
@BindView(R.id.quanxuan)
CheckBox quanxuan;
@BindView(R.id.t_price)
TextView tPrice;
@BindView(R.id.buy)
Button buy;
private ShoppBean cartBean;
private OutterAdapater adapater;
@Override
protected void initData() {
persenter.getData("8545", "15722429925158545", getActivity());
}
@Override
protected ShoppPersenter<ShoppContract.ShoppView> CreatePersenter() {
return new ShoppPersenter<>();
}
@Override
protected int getLayout() {
return R.layout.tab_shoppingcart;
}
@Override
public void Shoppyoyo(final ShoppBean shoppBean) {
cartBean=shoppBean;
shoppRecy.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
adapater = new OutterAdapater(R.layout.outter, shoppBean.getResult());
shoppRecy.setAdapter(adapater);
adapater.setOnOutterListener(new OutterAdapater.onOutterListener() {
@Override
public void onOutterChaned() {
boolean outterAll=true;
int Num=0;
for (int i = 0; i <shoppBean.getResult().size() ; i++) {
// TODO: 2019/10/26 看商品是否选中状态
boolean onOutterchecked = shoppBean.getResult().get(i).isOnOutterchecked();
// TODO: 2019/10/26 循环商品的集合
for (int j = 0; j <shoppBean.getResult().get(i).getShoppingCartList().size() ; j++) {
// TODO: 2019/10/26 给商品设置是否选中状态,
boolean onInnerchecked = shoppBean.getResult().get(i).getShoppingCartList().get(j).isOnInnerchecked();
int count = shoppBean.getResult().get(i).getShoppingCartList().get(j).getCount();
int price = shoppBean.getResult().get(i).getShoppingCartList().get(j).getPrice();
if (onInnerchecked){
Num+=count*price;
}
outterAll=outterAll&onOutterchecked&onInnerchecked;
}
}
quanxuan.setChecked(outterAll);
tPrice.setText("总价"+Num);
}
});
}
@OnClick(R.id.quanxuan)
public void onViewClicked() {
// TODO: 2019/10/26 设置
boolean checked = quanxuan.isChecked();
// TODO: 2019/10/26 循环商家的集合
for (int i = 0; i <cartBean.getResult().size() ; i++) {
// TODO: 2019/10/26 给商家设置选中状态
cartBean.getResult().get(i).setOnOutterchecked(checked);
// TODO: 2019/10/26 遍历循环商品的集合
for (int j = 0; j <cartBean.getResult().get(i).getShoppingCartList().size() ; j++) {
// TODO: 2019/10/26 给商品设置选中状态
cartBean.getResult().get(i).getShoppingCartList().get(j).setOnInnerchecked(checked);
}
}
// TODO: 2019/10/26 刷新
adapater.notifyDataSetChanged();
}
}
加减器类
public class AddView extends LinearLayout {
@BindView(R.id.but_add)
Button butAdd;
@BindView(R.id.t_Num)
TextView tNum;
@BindView(R.id.but_jian)
Button butJian;
private int Num;
public AddView(Context context, AttributeSet attrs) {
super(context, attrs);
View inflate = LayoutInflater.from(context).inflate(R.layout.addview, this, true);
ButterKnife.bind(this,inflate);
}
public void setText(int count){
this.Num=count;
tNum.setText(Num+"");
}
@OnClick({R.id.but_add, R.id.but_jian})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.but_add:
Num++;
tNum.setText(Num+"");
if (setCount!=null){
setCount.Count(Num);
}
break;
case R.id.but_jian:
if (Num<1){
Toast.makeText(getContext(), "不可以再减了", Toast.LENGTH_SHORT).show();
}
Num++;
tNum.setText(Num+"");
if (setCount!=null){
setCount.Count(Num);
}
break;
}
}
public interface setCount{
void Count(int count);
}
setCount setCount;
public void setSetCount(AddView.setCount setCount) {
this.setCount = setCount;
}
}