A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

《MainActivity》
public class MainActivity extends AppCompatActivity implements MainView {

private RecyclerView recycler;
private CheckBox checkbox;
private TextView alllprice;
private TextView money;
private MainPresenterIml mainPresenterIml;
private ShopAdapter adapter;
private List<NewBean.DataBean> data1 = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    checkbox = (CheckBox) findViewById(R.id.checkbox_all);
    alllprice = (TextView) findViewById(R.id.tv_allprice);
    money = (TextView) findViewById(R.id.tv_money);
    recycler = findViewById(R.id.recycler_01);
    mainPresenterIml = new MainPresenterIml(new MainModelIml(), (MainView) this);
    mainPresenterIml.doShopCar(0, "http://172.17.8.100/ks/product/getCarts?uid=51");
    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recycler.setLayoutManager(layoutManager);
    adapter = new ShopAdapter(this);
    recycler.setAdapter(adapter);
    //全选方法
    getChecked();

}

private void getChecked() {
    checkbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean mchecked = checkbox.isChecked();
            double alllPrice = 0;
            int allNum = 0;
            for (int a = 0; a < data1.size(); a++) {
                data1.get(a).setChecked(mchecked);
                List<NewBean.DataBean.ListBean> list2 = data1.get(a).getList();
                for (int b = 0; b < list2.size(); b++) {
                    list2.get(b).setChecked(mchecked);
                    if (mchecked) {
                        double price = list2.get(b).getPrice();
                        int num = list2.get(b).getNum();
                        alllPrice = alllPrice + price * num;
                        allNum = allNum + num;
                    } else {
                        alllPrice = 0;
                        allNum=0;
                    }
                }
            }
            alllprice.setText(alllPrice + "");
            money.setText("去结算("+allNum+")");
            adapter.setList(data1);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mainPresenterIml.destory();
}

@Override
public void success(int type, String data) {
    if (type == 0) {
        NewBean newBean = new Gson().fromJson(data, NewBean.class);
        Log.i("aaa", newBean.toString());
        Gson gson = new Gson();
        NewBean newBean1 = gson.fromJson(data, NewBean.class);
        data1 = newBean1.getData();
        data1.remove(0);
        adapter.setList(data1);
    }

}

@Override
public void fail(int type, String error) {

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
}

《ShopAdapter 》
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {
private Context context;
private ArrayList<NewBean.DataBean> list =new ArrayList<>();

public ShopAdapter(Context context) {
    this.context = context;
}

@NonNull
@Override
public ShopAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(context, R.layout.itme_03, null);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ShopAdapter.ViewHolder viewHolder, int i) {
     viewHolder.title01.setText(list.get(i).getSellerName());
     viewHolder.check.setChecked(list.get(i).isChecked());
    List<NewBean.DataBean.ListBean> listShop = list.get(i).getList();//获取商家下对应得商品的集合
    ShopCarItemAdapter shopCarItemAdapter = new ShopCarItemAdapter(context, listShop);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    viewHolder.mrecyler.setLayoutManager(linearLayoutManager);//一定要记住设置布局管理器
    viewHolder.mrecyler.setAdapter(shopCarItemAdapter);
}

@Override
public int getItemCount() {
    return list.size();
}

public void setList(List<NewBean.DataBean> list) {
    this.list= (ArrayList<NewBean.DataBean>) list;
    notifyDataSetChanged();
}


public class ViewHolder extends RecyclerView.ViewHolder {



    CheckBox check;
    TextView title01;
    RecyclerView mrecyler;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        check = (CheckBox)itemView.findViewById(R.id.checkbox_01);
        title01 =(TextView) itemView.findViewById(R.id.tv_title_01);
        mrecyler =(RecyclerView) itemView.findViewById(R.id.recycler_1);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
}

《ShopCarItemAdapter 》
public class ShopCarItemAdapter extends RecyclerView.Adapter<ShopCarItemAdapter.ShopCarItemViewHolder>{
private Context context;
private List<NewBean.DataBean.ListBean> listShop = new ArrayList<>();

public ShopCarItemAdapter(Context context, List<NewBean.DataBean.ListBean> listShop) {
    this.context = context;
    this.listShop = listShop;
}
@NonNull
@Override
public ShopCarItemAdapter.ShopCarItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(context, R.layout.itme_01, null);
    ShopCarItemViewHolder holder = new ShopCarItemViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ShopCarItemAdapter.ShopCarItemViewHolder shopCarItemViewHolder, int i) {
    Glide.with(context).load(listShop.get(i).getImages()).into(shopCarItemViewHolder.mImage);
    shopCarItemViewHolder.mTitle.setText(listShop.get(i).getTitle());
    shopCarItemViewHolder.mPrice.setText(listShop.get(i).getPrice() + "");
    shopCarItemViewHolder.mCheckBox.setChecked(listShop.get(i).isChecked());
    shopCarItemViewHolder.edit.setNum(listShop.get(i).getNum());
}

@Override
public int getItemCount() {
    return listShop.size();
}

public class ShopCarItemViewHolder extends RecyclerView.ViewHolder {

    EditViewal edit;
    ImageView mImage;
    TextView mTitle;
    TextView mPrice;
    CheckBox mCheckBox;

    public ShopCarItemViewHolder(@NonNull View itemView) {
        super(itemView);
        mImage = (ImageView) itemView.findViewById(R.id.iv_shop);
        mTitle = (TextView) itemView.findViewById(R.id.tv_title);
        mPrice = (TextView) itemView.findViewById(R.id.tv_price);
        mCheckBox =(CheckBox)itemView.findViewById(R.id.checkbox);
        edit =(EditViewal) itemView.findViewById(R.id.edit_all);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
}
---------------------
【转载,仅作分享,侵删】
作者:偏执青年
原文:https://blog.csdn.net/weixin_44666694/article/details/89059420
版权声明:本文为博主原创文章,转载请附上博文链接!

1 个回复

倒序浏览
奈斯,感谢分享!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马