activity
package com.bwei.hanchen20190308;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import com.bwei.hanchen20190308.adapter.FuAdapter;
import com.bwei.hanchen20190308.bean.GwcBean;
import com.bwei.hanchen20190308.presenter.GwPresenter;
import com.bwei.hanchen20190308.view.GwView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class GwcActivity extends AppCompatActivity implements GwView {
@BindView(R.id.rec)
RecyclerView rec;
@BindView(R.id.quanx)
CheckBox quanx;
@BindView(R.id.zj)
TextView zj;
@BindView(R.id.zshu)
Button zshu;
private GwPresenter gwPresenter;
private GwcBean gwcBean;
private FuAdapter fuAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gwc);
ButterKnife.bind(this);
gwPresenter = new GwPresenter(this);
gwPresenter.getModelDate("51");
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
rec.setLayoutManager(layoutManager);
}
@Override
public void getViewDate(Object sn) {
gwcBean = (GwcBean) sn;
fuAdapter = new FuAdapter(this, gwcBean);
rec.setAdapter(fuAdapter);
quanx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用计算价格
checer(quanx.isChecked());
//刷新适配器
fuAdapter.notifyDataSetChanged();
}
});
//接口回调遍历状态根据判断状态计算总价
fuAdapter.setMaiBackListenr(new FuAdapter.MaiJBackListenr(){
@Override
public void callBack(List<GwcBean.DataBean> list){
double price=0;
int shopsun=0;
int bxz=0;
for (int i=0;i<list.size();i++){
List<GwcBean.DataBean.ListBean> list1 = list.get(i).getList();
for (int b=0;b<list1.size();b++){
//所有的数量
shopsun+=Integer.valueOf(list1.get(b).getNum());
if (list1.get(b).isChecked()){
price=price+(Double.valueOf(list1.get(b).getPrice())*Integer.valueOf(list1.get(b).getNum()));
//选中的数量
bxz=bxz+Integer.valueOf(list1.get(b).getNum());
}
}
}
//如果所有的数量大于选中的状态
if (shopsun>bxz){
quanx.setChecked(false);
}else {
quanx.setChecked(true);
}
zj.setText("合计:" + price);
zshu.setText("去结算(" + bxz + ")");
}
});
}
///全选计算总价
private void checer(boolean bool){
double totalPrice = 0;
int num = 0;
for (int a = 0; a <gwcBean.getData().size();a++){
//遍历商家,改变状态
GwcBean.DataBean dataBean = gwcBean.getData().get(a);
dataBean.setChecked(bool);
List<GwcBean.DataBean.ListBean> listAll =gwcBean.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())*Integer.valueOf(listAll.get(i).getNum()));
num = num + Integer.valueOf(listAll.get(i).getNum());
}
}
if (bool){
zj.setText("合计:" + totalPrice);
zshu.setText("去结算(" + num + ")");
} else {
zj.setText("合计:0.00");
zshu.setText("去结算(0)");
}
}
//内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
if (gwPresenter!=null){
gwPresenter.Distory();
}
}
}
主适配器
package com.bwei.hanchen20190308.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.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.bwei.hanchen20190308.R;
import com.bwei.hanchen20190308.bean.GwcBean;
import java.util.List;
public class FuAdapter extends RecyclerView.Adapter<FuAdapter.ViewHodler> {
Context context;
GwcBean gwcBean;
public FuAdapter(Context context, GwcBean gwcBean) {
this.context = context;
this.gwcBean = gwcBean;
}
@NonNull
@Override
public FuAdapter.ViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View mview;
mview = View.inflate(viewGroup.getContext(),R.layout.item_yz,null);
return new ViewHodler(mview);
}
@Override
public void onBindViewHolder(@NonNull final FuAdapter.ViewHodler viewHodler, final int i) {
//设置值
viewHodler.t1.setText(gwcBean.getData().get(i).getSellerName());
//全选状态
viewHodler.sjxz.setChecked(gwcBean.getData().get(i).isChecked());
//提供数据
List<GwcBean.DataBean.ListBean> list = gwcBean.getData().get(i).getList();
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
viewHodler.rfs.setLayoutManager(layoutManager);
//创建适配器不可提全局
final ZiAdapter ziAdapter = new ZiAdapter(context,list);
viewHodler.rfs.setAdapter(ziAdapter);
//单点状态
viewHodler.sjxz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置点点击的CheckBox状态
gwcBean.getData().get(i).setChecked(viewHodler.sjxz.isChecked());
//设置跟随当前的商家选中状态控制子列表的选中状态
ziAdapter.seleAll(viewHodler.sjxz.isChecked());
}
});
//接口回调
ziAdapter.setMaiBackListenr(new ZiAdapter.MaiBackListenr() {
@Override
public void callBack() {
if (maiBackListenr != null) {
maiBackListenr.callBack(gwcBean.getData());
}
//告诉activity商品的数量被改变
List<GwcBean.DataBean.ListBean> list1 = gwcBean.getData().get(i).getList();
boolean isA=true;
for (GwcBean.DataBean.ListBean bean: list1) {
//子商品的状态循环遍历判断
if (!bean.isChecked()){
isA=false;
break;
}
}
//给商家设置CheCkBox状态
viewHodler.sjxz.setChecked(isA);
//接口的状态也同步下
gwcBean.getData().get(i).setChecked(isA);
}
});
}
@Override
public int getItemCount(){
return gwcBean.getData().size();
}
public class ViewHodler extends RecyclerView.ViewHolder {
RecyclerView rfs;
TextView t1;
CheckBox sjxz;
public ViewHodler(@NonNull View itemView) {
super(itemView);
rfs= itemView.findViewById(R.id.rfs);
t1= itemView.findViewById(R.id.t1);
sjxz= itemView.findViewById(R.id.sjxz);
}
}
//声明接口
private MaiJBackListenr maiBackListenr;
public void setMaiBackListenr(MaiJBackListenr maiBackListenr) {
this.maiBackListenr = maiBackListenr;
}
//定义
public interface MaiJBackListenr{
void callBack(List<GwcBean.DataBean> list);
}
}
子适配器
package com.bwei.hanchen20190308.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.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.bwei.hanchen20190308.DinView;
import com.bwei.hanchen20190308.R;
import com.bwei.hanchen20190308.bean.GwcBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
public class ZiAdapter extends RecyclerView.Adapter<ZiAdapter.ViewHodler> {
Context context;
List<GwcBean.DataBean.ListBean> list;
public ZiAdapter(Context context,List<GwcBean.DataBean.ListBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ZiAdapter.ViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View mview;
mview = View.inflate(viewGroup.getContext(),R.layout.item_tz,null);
return new ViewHodler(mview);
}
@Override
public void onBindViewHolder(@NonNull final ZiAdapter.ViewHodler viewHodler, final int i){
//设置值
viewHodler.t2.setText(list.get(i).getTitle());
viewHodler.t3.setText(list.get(i).getPrice());
viewHodler.ima.setImageURI(list.get(i).getImages().split("\\|")[0].replace("https", "http"));
//全选状态
viewHodler.spzx.setChecked(list.get(i).isChecked());
//选中联动
viewHodler.spzx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
list.get(i).setChecked(isChecked);
if (maiBackListenr != null) {
maiBackListenr.callBack();
}
}
});
//设置自定义view
viewHodler.s1.setCallBackListenr(new DinView.CallBackListenr() {
@Override
public void callBack() {
if (maiBackListenr != null) {
//回传
maiBackListenr.callBack();
}
}
});
//给自定义View传值
viewHodler.s1.setData(ZiAdapter.this,list,i);
}
///联动主适配器全选
public void seleAll(boolean isAll){
//遍历集合选中状态
for (GwcBean.DataBean.ListBean listBean: list){
listBean.setChecked(isAll);
}
notifyDataSetChanged();
}
@Override
public int getItemCount(){
return list.size();
}
public class ViewHodler extends RecyclerView.ViewHolder {
SimpleDraweeView ima;
TextView t2;
TextView t3;
DinView s1;
CheckBox spzx;
public ViewHodler(@NonNull View itemView) {
super(itemView);
t2= itemView.findViewById(R.id.t2);
ima= itemView.findViewById(R.id.image);
t3= itemView.findViewById(R.id.t3);
s1 = itemView.findViewById(R.id.num);
spzx = itemView.findViewById(R.id.spxz);
}
}
//实现接口
private MaiBackListenr maiBackListenr;
public void setMaiBackListenr(MaiBackListenr maiBackListenr) {
this.maiBackListenr = maiBackListenr;
}
public interface MaiBackListenr{
void callBack();
}
}
自定义View
package com.bwei.hanchen20190308;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.bwei.hanchen20190308.adapter.ZiAdapter;
import com.bwei.hanchen20190308.bean.GwcBean;
import java.util.List;
import butterknife.BindView;
public class DinView extends LinearLayout implements View.OnClickListener {
private Button jian,jia;
private EditText eid;
private int num;
private List<GwcBean.DataBean.ListBean> mlist;
private ZiAdapter ziAdapter;
int Position;
public void setData(ZiAdapter ziAdapter,List<GwcBean.DataBean.ListBean> mlist,int i){
this.mlist=mlist;
this.ziAdapter=ziAdapter;
this.Position=i;
eid.setText(mlist.get(i).getNum());
}
public DinView(Context context) {
super(context);
}
public DinView(Context context, AttributeSet attrs) {
super(context, attrs);
//绑定布局
LayoutInflater.from(context).inflate(R.layout.gou_sum,this);
jian = findViewById(R.id.jian);
jia = findViewById(R.id.jia);
eid = findViewById(R.id.eid);
//点击监听
jia.setOnClickListener(this);
jian.setOnClickListener(this);
//输入框展示内容
eid.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
num = Integer.parseInt(s.toString());
//TODO:改变数量
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public DinView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DinView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onClick(View v) {
switch (v.getId()){
//点击增加数量
case R.id.jia:
num++;
eid.setText(num+"");
mlist.get(Position).setNum(String.valueOf(num));
callBackListenr.callBack();
ziAdapter.notifyItemChanged(Position);
break;
//点击减少数量
case R.id.jian:
if (num>1){
num--;
}
eid.setText(num+"");
mlist.get(Position).setNum(String.valueOf(num));
callBackListenr.callBack();
ziAdapter.notifyItemChanged(Position);
break;
}
}
//实现接口
private CallBackListenr callBackListenr;
public void setCallBackListenr(CallBackListenr callBackListenr) {
this.callBackListenr = callBackListenr;
}
public interface CallBackListenr{
void callBack();
}
}