package soexample.umeng.com.mnzk3; import android.content.Context; import android.content.Intent; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import soexample.umeng.com.mnzk3.gouwuc.GouwucheAdapter; import soexample.umeng.com.mnzk3.bean.GouwucheBean; import soexample.umeng.com.mnzk3.liushi.Fedui; import soexample.umeng.com.mnzk3.mvp.view.AgeInter; import soexample.umeng.com.mnzk3.net.HttpHelper; public class MainActivitypersenter extends AgeInter implements View.OnClickListener{ private String url = "http://www.zhaoapi.cn/product/getCarts?uid=71"; private ImageView ivCricle; private TextView allPriceTxt; private TextView sunPrice; private List<GouwucheBean.DataBean> list = new ArrayList<>(); private GouwucheAdapter mShopSellerAdapter; private RecyclerView mRecyclerView; @Override public int getLayoutId() { return R.layout.activity_main; } @Override public void initdata() { super.initdata(); mRecyclerView = (RecyclerView) get(R.id.layout_top1); ivCricle = (ImageView) get(R.id.iv_cricle); allPriceTxt = (TextView) get(R.id.all_price); sunPrice = (TextView) get(R.id.sum_price_txt); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(linearLayoutManager); mShopSellerAdapter = new GouwucheAdapter(context); mRecyclerView.setAdapter(mShopSellerAdapter); odhttp(); get(R.id.layout_all).setOnClickListener(this); mShopSellerAdapter.setListener(new GouwucheAdapter.ShopCallBackListener() { @Override public void callBack(List<GouwucheBean.DataBean> list) { double allPrice = 0; int num = 0; int numAll = 0; for (int a = 0; a < list.size(); a++) { List<GouwucheBean.DataBean.ListBean> listAll = list.get(a).getList();//获取商家里商品 for (int i = 0; i < listAll.size(); i++) { numAll = numAll + listAll.get(i).getNum(); if (listAll.get(i).isCheck()) {//取选中的状态 allPrice = allPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum()); num = num + listAll.get(i).getNum(); } } } if (num < numAll) {//不是全部选中 ivCricle.setImageResource(R.drawable.cricle_no); isClick = true; } else { //是全部选中 ivCricle.setImageResource(R.drawable.cricle_yes); isClick = false; } //改变价格和个数 allPriceTxt.setText("合计:" + allPrice); sunPrice.setText("去结算(" + num + ")"); } }); } private void odhttp() { new HttpHelper().get(url).result(new HttpHelper.HttpListener() { @Override public void success(String data) { if(data.contains("<")){ odhttp(); return; } GouwucheBean bean = new Gson().fromJson(data, GouwucheBean.class); list = bean.getData(); list.remove(0); mShopSellerAdapter.setList(list); } @Override public void fail(String error) { } }); } private Context context; @Override public void getContext(Context context) { this.context = context; } private boolean isClick; @Override public void onClick(View view) { switch (view.getId()) { case R.id.layout_all://全选 if (isClick) { ivCricle.setImageResource(R.drawable.cricle_yes); isClick = false; checkSeller(true); } else { ivCricle.setImageResource(R.drawable.cricle_no); isClick = true; checkSeller(false); } break; } } private void checkSeller(boolean bool){ double allPrice=0; int num=0; for (int a=0;a<list.size();a++){ List<GouwucheBean.DataBean.ListBean> listAll = list.get(a).getList(); for (int i = 0; i <listAll.size() ; i++) { listAll.get(i).setCheck(bool); allPrice=allPrice+(listAll.get(i).getPrice()*listAll.get(i).getNum()); num=num+listAll.get(i).getNum(); } } if(bool){ allPriceTxt.setText("合计:"+allPrice); sunPrice.setText("去结算("+num+")"); }else{ allPriceTxt.setText("合计:0.00"); sunPrice.setText("去结算(0)"); } mShopSellerAdapter.notifyDataSetChanged(); } //搜索Butnf @Override public void oned() { super.oned(); context.startActivity(new Intent(context,Fedui.class)); } // @Override // public void onimg(View view) { // super.onimg(view); // ivCricle = (ImageView) view; // // } } -----------------------------------------------------------------------------
package soexample.umeng.com.mnzk3.gouwuc; import android.content.Context; import android.content.Intent; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import soexample.umeng.com.mnzk3.R; import soexample.umeng.com.mnzk3.bean.GouwucheBean; import soexample.umeng.com.mnzk3.liushi.Fedui; public class GouwucheAdapter extends RecyclerView.Adapter<GouwucheAdapter.sgouwuche> { private List<GouwucheBean.DataBean> list = new ArrayList<>(); private Context context; public GouwucheAdapter(Context context) { this.context = context; } @Override public sgouwuche onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.gouwu, null); sgouwuche sgouwuche = new sgouwuche(view); return sgouwuche; } @Override public void onBindViewHolder(sgouwuche holder, int position) { holder.sellerName.setText(list.get(position).getSellerName());//设置商家的名字 GouwuShop gouwuche = new GouwuShop(list.get(position).getList(),context); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); holder.mRecyclerView.setLayoutManager(linearLayoutManager); holder.mRecyclerView.setAdapter(gouwuche); holder.mRecyclerView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { context.startActivity(new Intent(context,Fedui.class)); } }); gouwuche.setListener(new GouwuShop.ShopCallBackListener() { @Override public void callBack() { //从商品适配里回调回来 listener.callBack(list); } }); } @Override public int getItemCount() { return list.size(); } public void setList(List<GouwucheBean.DataBean> list) { this.list = list; notifyDataSetChanged(); } public class sgouwuche extends RecyclerView.ViewHolder { private final TextView sellerName; private final RecyclerView mRecyclerView; public sgouwuche(View itemView) { super(itemView); sellerName = (TextView) itemView.findViewById(R.id.seller_name); mRecyclerView = (RecyclerView) itemView.findViewById(R.id.seller_recyclerview); } } private ShopCallBackListener listener; public void setListener(ShopCallBackListener listener) { this.listener = listener; } public interface ShopCallBackListener { void callBack(List<GouwucheBean.DataBean> list); } }
-----------------------------------------------------------
package soexample.umeng.com.mnzk3.gouwuc; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.squareup.picasso.Picasso; import java.util.ArrayList; import java.util.List; import soexample.umeng.com.mnzk3.R; import soexample.umeng.com.mnzk3.bean.GouwucheBean; public class GouwuShop extends RecyclerView.Adapter<GouwuShop.sgoushop> { private List<GouwucheBean.DataBean.ListBean> list = new ArrayList<>(); private Context context; public GouwuShop(List<GouwucheBean.DataBean.ListBean> list, Context context) { this.list = list; this.context = context; } @Override public sgoushop onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.goushop, null); sgoushop sgouwuche = new sgoushop(view); return sgouwuche; } @Override public void onBindViewHolder(sgoushop holder, final int i) { Picasso.with(context).load(list.get(i).getImages().split("\\|")[0]).fit().into(holder.carImage); holder.carTitle.setText(list.get(i).getTitle()); holder.carPrice.setText(list.get(i).getPrice() + ""); if (list.get(i).isCheck()) {//选中 holder.carCricle.setImageResource(R.drawable.cricle_yes); } else { holder.carCricle.setImageResource(R.drawable.cricle_no); } //点击商品选中 holder.carCricle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (list.get(i).isCheck()) { list.get(i).setCheck(false); } else { list.get(i).setCheck(true); } notifyItemChanged(i); listener.callBack(); } }); //设置自定义View里的Edit holder.shop.setData(this, list, i); holder.shop.setOnCallBack(new ShopCarPriceLayout.CallBackListener() { @Override public void callBack() { listener.callBack(); } }); } @Override public int getItemCount() { return list.size(); } public class sgoushop extends RecyclerView.ViewHolder { private final TextView carTitle; private final ImageView carImage; private final TextView carPrice; private final ImageView carCricle; ShopCarPriceLayout shop; public sgoushop(View itemView) { super(itemView); carImage = (ImageView) itemView.findViewById(R.id.car_image); carTitle = (TextView) itemView.findViewById(R.id.car_title); carPrice = (TextView) itemView.findViewById(R.id.car_price); carCricle = (ImageView) itemView.findViewById(R.id.car_cricle); shop = (ShopCarPriceLayout) itemView.findViewById(R.id.shopcarpricelayout); } } private ShopCallBackListener listener; public void setListener(ShopCallBackListener listener) { this.listener = listener; } public interface ShopCallBackListener { void callBack(); } }
------------------------------------------------
package soexample.umeng.com.mnzk3.gouwuc; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import soexample.umeng.com.mnzk3.R; import soexample.umeng.com.mnzk3.bean.GouwucheBean; public class ShopCarPriceLayout extends RelativeLayout implements View.OnClickListener { private EditText editCar; public ShopCarPriceLayout(Context context, List<GouwucheBean.DataBean.ListBean> list) { super(context); init(context); } public ShopCarPriceLayout(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public ShopCarPriceLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { View view = View.inflate(context, R.layout.jia, null); ImageView addIamge = (ImageView) view.findViewById(R.id.add_car); ImageView jianIamge = (ImageView) view.findViewById(R.id.jian_car); editCar = (EditText) view.findViewById(R.id.edit_shop_car); addIamge.setOnClickListener(this); jianIamge.setOnClickListener(this); addView(view); } private int num; private Context context; @Override public void onClick(View view) { switch (view.getId()) { case R.id.add_car://增加 num++; editCar.setText(num + ""); list.get(position).setNum(num); listener.callBack(); shopSellerCarAdapter.notifyItemChanged(position); break; case R.id.jian_car://减 if (num > 1) { num--; } else { toast("我是有底线的啊"); } editCar.setText(num + ""); list.get(position).setNum(num); listener.callBack(); shopSellerCarAdapter.notifyItemChanged(position); break; } } private void toast(String msg) { Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); } //传递的数据 private List<GouwucheBean.DataBean.ListBean> list = new ArrayList<>(); private int position; private GouwuShop shopSellerCarAdapter; public void setData(GouwuShop shopSellerCarAdapter, List<GouwucheBean.DataBean.ListBean> list, int i) { this.list = list; this.shopSellerCarAdapter = shopSellerCarAdapter; position = i; num = list.get(i).getNum(); editCar.setText(num + ""); } private CallBackListener listener; public void setOnCallBack(CallBackListener listener) { this.listener = listener; } public interface CallBackListener { void callBack(); } }