//自定义view加减实现 public class customview extends RelativeLayout implements View.OnClickListener { private Button addo; private Button removeo; private TextView textview; private Integer num; private int postion; Context context; Listlist; private GoodsAdapter goodsAdapter; public customview(Context context) { super(context); intit(context); } public customview(Context context, AttributeSet attrs) { super(context, attrs); intit(context); } public customview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); intit(context); } public customview(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); intit(context); } private void intit(Context context) { View inflate = View.inflate(context, R.layout.add_bar, null); addo = inflate.findViewById(R.id.add); removeo = inflate.findViewById(R.id.remove); textview = inflate.findViewById(R.id.textview); addo.setOnClickListener(this); removeo.setOnClickListener(this); addView(inflate); textview.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { num = Integer.parseInt(charSequence.toString()); } @Override public void afterTextChanged(Editable editable) { } }); } public void setdata(GoodsAdapter goodsAdapter, List list, int i){ this.goodsAdapter =goodsAdapter; this.list=list; postion=i; textview.setText(list.get(i).getNum()); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.add: num++; textview.setText(""+num); list.get(postion).setNum(String.valueOf(num)); mCallBackListener.callBack(); goodsAdapter.notifyItemChanged(postion); break; case R.id.remove: if (num<1){ Toast.makeText(context,"地了",Toast.LENGTH_SHORT).show(); }else { num--; textview.setText(""+num); list.get(postion).setNum(String.valueOf(num)); mCallBackListener.callBack(); goodsAdapter.notifyItemChanged(postion); } break; } } private CallBackListener mCallBackListener; public void setOnCallBack(CallBackListener listener) { this.mCallBackListener = listener; } public interface CallBackListener { void callBack(); } }
//自定义加减布局
//商品适配器
public class GoodsAdapter extends RecyclerView.Adapter{ List list; Context context; public GoodsAdapter(Context context,List list) { this.list = list; this.context=context; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.recyitem_layout, viewGroup, false)); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) { viewHolder.name.setText(list.get(i).getTitle()); viewHolder.price.setText(list.get(i).getPrice()); Glide.with(context).load(list.get(i).getImages().split("\\|")[0].replace("https","http")).into(viewHolder.imageviewitem); viewHolder.checkitem.setChecked(list.get(i).isCheck()); viewHolder.checkitem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { list.get(i).setCheck(b); if (mShopCallBackListener!=null){ mShopCallBackListener.callBack(); } } }); viewHolder.custom.setdata(this,list,i); viewHolder.custom.setOnCallBack(new customview.CallBackListener() { @Override public void callBack() { if (mShopCallBackListener!=null){ mShopCallBackListener.callBack(); } } }); } @Override public int getItemCount() { return list.size(); } public void setAllCheck(boolean isAllcheck){ for (CarBean.DataBean.ListBean listBean:list ) { listBean.setCheck(isAllcheck); } notifyDataSetChanged(); } public class ViewHolder extends RecyclerView.ViewHolder { private final customview custom; private ImageView imageviewitem; private TextView name,price; private CheckBox checkitem; public ViewHolder(@NonNull View itemView) { super(itemView); checkitem= itemView.findViewById(R.id.checkboxitem); imageviewitem= itemView.findViewById(R.id.imageviewItem); price= itemView.findViewById(R.id.textprice); name= itemView.findViewById(R.id.texttitle); custom =itemView.findViewById(R.id.custom); } } private ShopCallBackListener mShopCallBackListener; public void setListener(ShopCallBackListener listener) { this.mShopCallBackListener = listener; } public interface ShopCallBackListener { void callBack(); }
//商家适配器
public class CarAdapter extends RecyclerView.Adapter{ List data; Context context; public CarAdapter(Context context, List data) { this.context=context; this.data=data; } @NonNull @Override public CarAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View inflate = LayoutInflater.from(context).inflate(R.layout.car_layout, viewGroup, false); ViewHolder viewHolder = new ViewHolder(inflate); return viewHolder; } @Override public void onBindViewHolder(@NonNull final CarAdapter.ViewHolder viewHolder, final int i) { viewHolder.textView.setText(data.get(i).getSellerName()); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL); viewHolder.recyviewitem.setLayoutManager(linearLayoutManager); final List list = data.get(i).getList(); final GoodsAdapter goodsAdapter = new GoodsAdapter(context, list); viewHolder.recyviewitem.setAdapter(goodsAdapter); viewHolder.checkBox.setChecked(data.get(i).isCheck()); goodsAdapter.setListener(new GoodsAdapter.ShopCallBackListener() { @Override public void callBack() { if (mShopCallBackListener!=null){ mShopCallBackListener.callBack(data); } List list1 = data.get(i).getList(); boolean ischeckall=true; for (CarBean.DataBean.ListBean bean:list1 ) { if (!bean.isCheck()){ ischeckall=false; break; } viewHolder.checkBox.setChecked(ischeckall); list1.get(i).setCheck(ischeckall); } } }); viewHolder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { data.get(i).setCheck(viewHolder.checkBox.isChecked()); goodsAdapter.setAllCheck(viewHolder.checkBox.isChecked()); } }); } @Override public int getItemCount() { return data.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private CheckBox checkBox; private TextView textView; private final RecyclerView recyviewitem; public ViewHolder(@NonNull View itemView) { super(itemView); checkBox= itemView.findViewById(R.id.check); textView= itemView.findViewById(R.id.text); recyviewitem=itemView.findViewById(R.id.recyviewitem); } } private ShopCallBackListener mShopCallBackListener; public void setListener(ShopCallBackListener listener) { this.mShopCallBackListener = listener; } public interface ShopCallBackListener { void callBack(List list); } }
//最后的选中切换状态改变jiage
public class MainActivity extends AppCompatActivity implements ShopViewInter,View.OnClickListener { private RecyclerView recyview; private Listdata; private TextView goodssunm; private TextView sumprice; private CheckBox checkall; private CarAdapter carAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ShopPresenterBase shopPresenterBase = new ShopPresenterBase(this); shopPresenterBase.getShopModel(); recyview = findViewById(R.id.recyview); goodssunm = findViewById(R.id.sumgoods); sumprice = findViewById(R.id.sumprice); checkall = findViewById(R.id.checkall); checkall.setOnClickListener(this); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); recyview.setLayoutManager(linearLayoutManager); } @Override public void getViewData(Object viewdata) { CarBean carBean= (CarBean) viewdata; data = carBean.getData(); carAdapter = new CarAdapter(this, data); recyview.setAdapter(carAdapter); carAdapter.setListener(new CarAdapter.ShopCallBackListener() { @Override public void callBack(List list) { double totalPrice = 0; //勾选商品的数量,不是该商品购买的数量 int num = 0; //所有商品总数,和上面的数量做比对,如果两者相等,则说明全选 int totalNum = 0; for (int a=0;a listBeans = list.get(a).getList(); for (int i=0;i list = dataBean.getList(); for (int i=0;i