购物车Activity以及适配器
作者:互联网
《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) {
}
}
《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);
}
}
}
《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);
}
}
}
标签:itemView,get,适配器,private,购物车,context,Activity,new,public 来源: https://blog.csdn.net/weixin_44666694/article/details/89059420