Listvie简单实现购物车功能
本文实例为大家分享了Listvie简单实现购物车的具体代码,供大家参考,具体内容如下
1.自定义view实现加减
publicclassMyViewextendsLinearLayoutimplementsView.OnClickListener{ privateOnAmountChangeListenermListener; privateintcount; privateTextViewnumText; publicMyView(Contextcontext){ this(context,null); } publicMyView(Contextcontext,@NullableAttributeSetattrs){ this(context,attrs,0); } publicMyView(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr){ super(context,attrs,defStyleAttr); Viewview=View.inflate(context,R.layout.add,this); initView(view); } publicvoidsetText(intnum){ numText.setText(""+num); } publicintgetText(){ getTextString(); returncount; } privatevoidinitView(Viewview){ ButtonaddBtn=view.findViewById(R.id.btn_add); ButtonjianBtn=view.findViewById(R.id.btn_jian); numText=view.findViewById(R.id.txt_num); getTextString(); addBtn.setOnClickListener(this); jianBtn.setOnClickListener(this); } publicvoidsetOnclickNum(OnAmountChangeListenermListener){ this.mListener=mListener; } @Override publicvoidonClick(Viewview){ getTextString(); switch(view.getId()){ caseR.id.btn_add: count++; break; caseR.id.btn_jian: if(count>0){ count--; } break; } numText.setText(""+count); if(mListener!=null){ mListener.onAmountChange(view,count); } } publicvoidgetTextString(){ StringnumStr=numText.getText().toString().trim(); count=Integer.parseInt(numStr); } publicinterfaceOnAmountChangeListener{ voidonAmountChange(Viewview,intamount); } }
2.适配器接口回调
publicclassMyAdapterextendsBaseAdapter{ privateListmList; privateContextcontext; privateGaroupClickgaroupClick; privateintindex; privateCountClickcountClick; publicMyAdapter(Contextcontext){ this.context=context; } @Override publicintgetCount(){ returnmList.size(); } publicvoidsetData(List mList){ this.mList=mList; notifyDataSetChanged(); } @Override publicObjectgetItem(inti){ returnmList.get(i); } @Override publiclonggetItemId(inti){ returni; } publicvoidsetOnCountClick(CountClickclick){ this.countClick=click; } publicvoidsetOnGroupclick(GaroupClickgroupclick){ this.garoupClick=groupclick; } @Override publicViewgetView(finalinti,Viewview,ViewGroupviewGroup){ finalViewHolderholder; if(view==null){ holder=newViewHolder(); view=View.inflate(context,R.layout.item,null); holder.childCheck=view.findViewById(R.id.cb_chid_checked); holder.txtPrice=view.findViewById(R.id.txt_price); holder.myView=view.findViewById(R.id.myview); view.setTag(holder); }else{ holder=(ViewHolder)view.getTag(); } finalTexttext=mList.get(i); holder.childCheck.setChecked(text.isChoose()); holder.txtPrice.setText(""+text.getPrice()); holder.myView.setText(text.getCount()); holder.myView.setOnclickNum(newMyView.OnAmountChangeListener(){ @Override publicvoidonAmountChange(Viewview,intamount){ text.setCount(amount); } }); booleanchecked=holder.childCheck.isChecked(); if(checked){ holder.myView.setOnclickNum(newMyView.OnAmountChangeListener(){ @Override publicvoidonAmountChange(Viewview,intamount){ if(countClick!=null){ countClick.onNumClick(i,amount); } } }); } holder.childCheck.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ text.setChoose(((CheckBox)view).isChecked()); if(garoupClick!=null){ garoupClick.onItemClick(i,((CheckBox)view).isChecked()); } } }); returnview; } classViewHolder{ CheckBoxchildCheck; TextViewtxtPrice; MyViewmyView; } publicinterfaceGaroupClick{ voidonItemClick(intposition,booleanisChoose); } publicinterfaceCountClick{ voidonNumClick(intposition,intcount); } }
3.调用
publicclassMainActivityextendsAppCompatActivityimplementsMyAdapter.GaroupClick,MyAdapter.CountClick{ privateButtonjiesuanBtn; privateListViewmListView; privateCheckBoxallCheck; privateTextViewpriceText; privateListmList=newArrayList<>(); privateintnumPrice=0; privateintcount=0; privateMyAdapteradapter; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } privatevoidinitData(){ for(inti=0;i<30;i++){ Texttxt=newText(); txt.setPrice(20); txt.setCount(1); mList.add(txt); } adapter=newMyAdapter(this); adapter.setData(mList); mListView.setAdapter(adapter); adapter.setOnGroupclick(this); adapter.setOnCountClick(this); } publicbooleanisAllChecked(){ for(Texttext:mList){ if(!text.isChoose()){//遍历集合,有一个不选returnfalse returnfalse; } } returntrue; } privatevoidinitView(){ jiesuanBtn=(Button)findViewById(R.id.btn_num); mListView=(ListView)findViewById(R.id.listview); allCheck=(CheckBox)findViewById(R.id.cb_all_checked); priceText=(TextView)findViewById(R.id.txt_price_sum); allCheck.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ if(mList.size()!=0){ if(allCheck.isChecked()){ for(inti=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。